Friday 21 July 2017

Sorting : POJO Particular attribute



Now, I am sorting Arraylist.

ABOVE OR EQUAL API LEVEL-24

STEP-1 : 

frontAL.sort(Comparator.comparingDouble(ApiToViewModel::getPrice).reversed()); 

BELOW API LEVEL-24
STEP-1 :
Collections.sort(arraylist_object, new ComparatorClass());
STEP-2:

public class ComparatorClass implements Comparator<ApiToViewModel> {
    public int compare(ApiToViewModel p1, ApiToViewModel p2) {
        // FOR DESCENDING ORDER        if (p1.getPrice() < p2.getPrice()) return 1;
        if (p1.getPrice() > p2.getPrice()) return -1;
        // FOR ASCENDING ORDER        /*if (p1.getPrice() < p2.getPrice()) return -1;        if (p1.getPrice() > p2.getPrice()) return 1;        */        return 0;
    }
}


22

No comments:

Post a Comment