Sorting ArrayList of Objects by Object attribute

You need to write a Comparator<MyObject> and use Collections.sort(List<T>, Comparator<? super T> to sort your List.

Or else, your MyObject can also implements Comparable<MyObject>, defining a natural ordering that compares on your specific attribute, and then use Collections.sort(List<T> instead.

See also

Related questions

On sorting List on various criteria:

On Comparator and Comparable

Leave a Comment