I am having an Arraylist of Objects. Those object have an attribute or datatype - 'String'. I need to sort the Arraylist by that string. How to achieve this?
You need to write a Comparator<MyObject> and use Collections.sort(List<T>, Comparator<? super T> to sort your List.
Comparator<MyObject>
Collections.sort(List<T>, Comparator<? super T>
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.
MyObject
implements Comparable<MyObject>
Collections.sort(List<T>
On sorting List on various criteria:
On Comparator and Comparable
Comparator
Comparable
1.4m articles
1.4m replys
5 comments
56.8k users