Android sort arraylist by properties

Solution 1:

You need to implement a Comparator, for instance:

public class FishNameComparator implements Comparator<Fish>
{
    public int compare(Fish left, Fish right) {
        return left.name.compareTo(right.name);
    }
}

and then sort it like this:

Collections.sort(fishes, new FishNameComparator());

Solution 2:

You can simply do it by this code:

Collections.sort(list, new Comparator<Fish>() {
    public int compare(Fish o1, Fish o2) {
        return o1.name.compareTo(o2.name);
    }
});

Solution 3:

Kotlin code

list.sortWith(Comparator { o1, o2 -> o1.name!!.compareTo(o2.name!!) })