I am looking to implement a sort feature for my address book application.
I want to sort an ArrayList
. Contact
use this method:
private ArrayList sortList(ArrayList list) {
if (list != null && list.size() > 1) {
Collections.sort(list, new Comparator() {
public int compare(myClass o1, myClass o2) {
if (o1.getsortnumber() == o2.getsortnumber()) return 0;
return o1.getsortnumber() < o2.getsortnumber() ? 1 : -1;
}
});
}
return list;
}
`
and use: mySortedlist = sortList(myList);
No need to implement comparator in your class.
If you want inverse order swap 1
and -1