2

My array-list value is like below

[
{
    Distance=Distance: 16.6km,
    branch_desc=name 1,
},
{
    Distance=Distance: 5.4km,
    branch_desc=name 2,
},
{
    Distance=Distance: 13.4km,
    branch_desc=name 3,
}
]

How to sort the array-list to ascending and descending order compare with distance.I don't know how to sort the array list.Can any one know please help me to solve this issue.

Yugesh
  • 4,030
  • 9
  • 57
  • 97
  • 1
    had you tried Collection.sort()? – Biraj Zalavadia Sep 06 '13 at 12:34
  • 1
    *Collections : don't forget the `s`. – Arnaud Denoyelle Sep 06 '13 at 12:35
  • @ArnaudDenoyelle no, i dont know how to do that. – Yugesh Sep 06 '13 at 12:38
  • @PankajKumar above is an array-list not a j son array. – Yugesh Sep 06 '13 at 12:40
  • @Yugesh then please provide more information on the type of the elements. Can you give the class definition? – Vincent van der Weele Sep 06 '13 at 12:44
  • @Yugesh then there are too many answers avaible on SO. http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property – Pankaj Kumar Sep 06 '13 at 13:06
  • It is very simple For Ascending : Collections.sort(mArrayList, new OutcomeAscComparator()); public class OutcomeAscComparator implements Comparator { public int compare(Select left, Select right) { return right.getOutcome().compareTo(left.getOutcome()); } } – Ahmad Arslan Feb 11 '14 at 05:32

3 Answers3

12

See Collections#sort(List<T> list). It helps you to sort any list of T which implements Comparable.

If your class does not implement Comparable, you can provide a Comparator and call static void <T> sort(List<T> list, Comparator<? super T> c).

Example with a small class:

public class Item implements Comparable<Item>{

  private Integer number;
  private Double distance;

  //Getters & Setters
  ...

  @Override
  public int compareTo(Item o) {
    return this.distance > o.distance? -1 : 1;
  }
}

As it implements Comparable, you can sort a list like this :

List<Item> myItemList = ... ;
Collections.sort(myItemList);
//Now, myItemList is sorted

OR you can provide a Comparator to sort items on any criteria :

List<Item> list = ...;
//Here, I provide a Comparator to sort Items by number.
Collections.sort(list, new Comparator<Item>() {
  @Override
  public int compare(Item o1, Item o2) {
    return o2.getNumber() - o1.getNumber();
  }
});
Arnaud Denoyelle
  • 29,980
  • 16
  • 92
  • 148
3

Try This Code

List<List<Integer>> list = Arrays.asList(Arrays.asList(10, 5, 4), 
        Arrays.asList(1, 2, 3), Arrays.asList(5, 6, 7));
    for (List<Integer> l : list) {
    Collections.sort(l);
    }

Collections.sort(list, new Comparator<List<Integer>>() {
    public int compare(List<Integer> o1, List<Integer> o2) {
        return o1.get(0).compareTo(o2.get(0));
    }
});
System.out.println(list);

String Sort

Collections.sort(list, new Comparator<String>() {
        @Override
        public int compare(String s1, String s2) {
            return s1.compareToIgnoreCase(s2);
        }
    });
Developer
  • 6,292
  • 19
  • 55
  • 115
1

I suggest you to create a dedicated Comparator class extension. You can whether create one single class and use a boolean to specify if this should be ascending or descending order, or create one class for each order.

The difference will reside in your implementation(s) of compare(T, T), because this is where you will actually compare objects with criteria you choose, which is in your case the value of the field Distance. Then you call Collections.sort(ArrayList, Comparator) to sort the ArrayList.

Using one single class

MyComparator comp = ... // You can even pass the boolean at instanciation
comp.setAscending(true); // 'false' for descending

Collections.sort(list, comp); // Sort is performed - order depends on boolean

Using 2 casses

AscendingComparator comp1 ...
DescendingComparator comp2 ...

Collections.sort(list, comp1); // Sort ascending
Collections.sort(list, comp2); // Sort descending
Mickäel A.
  • 9,012
  • 5
  • 54
  • 71
  • how to use hash map array list in this comparator – Yugesh Sep 06 '13 at 13:30
  • You mean an `HashMap` of `ArrayList` ? – Mickäel A. Sep 06 '13 at 13:31
  • am get values from j son put in hash map and then i add the hash map values in array list. – Yugesh Sep 06 '13 at 13:33
  • 1
    To sort the `ArrayList`, just use the code above and replace *list* with the name of your `ArrayList`. Of course you have to code at least one Comparator class to have the sort to work. – Mickäel A. Sep 06 '13 at 13:43
  • To sort a `HashMap` it seems like you have to use [another solution](http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java), which is in fact very similar to the code I gave you. – Mickäel A. Sep 06 '13 at 13:45