Search This Blog

Showing posts with label Comparator. Show all posts
Showing posts with label Comparator. Show all posts

Tuesday, June 4, 2013

Sorting the ArrayList by Name and Value in android

Sorting the ArrayList by String comparison by the Following code.

Collections.sort(name, new Comparator<YOURARRAYLISTNAME>() {
    @Override
    public int compare(Invitation lhs, Invitation rhs) {
     return lhs.getName().compareToIgnoreCase(rhs.getName());
    }
   });


You can also compare Arraylist by INTEGER value by following code:


Collections.sort(value, new Comparator<YOURARRAYLISTNAME>() {
    @Override
    public int compare(Invitation lhs, Invitation rhs) {
                        return (lhs.expenditure < rhs.expenditure ? 1 : -1);
    }
   });