Kapil Bhandari Kapil Bhandari - 3 months ago 15
Android Question

Not able to sort my custom Adapter : Android

I have a activity in which i have setAdapter to a gridview .
Data are getting loaded perfectly fine .
I have a menu which have two options 1. Sort by Price and 2. Sort by Class

This is the function i have in my custom Adapter

public void sortlistbyPrice(){
Collections.sort(flightObjLists, new Comparator<FlightEntity>() {
@Override
public int compare(FlightEntity lhs, FlightEntity rhs) {
return lhs.getPrice()==rhs.getPrice()?1:0;
}
});
notifyDataSetChanged();

}


I am invoking this function from the Activity whenever i want to display items in a sorted way .

But unfortunately it is not happening , can anyone help me out over here ??

Answer

Could you test it for us?

public void sortlistbyPrice(){
    Collections.sort(flightObjLists, new Comparator<FlightEntity>() {
        @Override
        public int compare(FlightEntity lhs, FlightEntity rhs) {
            return lhs.getPrice() < rhs.getPrice() ? -1 : lhs.getPrice() == rhs.getPrice()? 1 : 0;
        }
    });
    notifyDataSetChanged();

}
Comments