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?
I am having an Arraylist of Objects. Those object have an attribute or datatype
Share
You need to write a
Comparator<MyObject>and useCollections.sort(List<T>, Comparator<? super T>to sort yourList.Or else, your
MyObjectcan alsoimplements Comparable<MyObject>, defining a natural ordering that compares on your specific attribute, and then useCollections.sort(List<T>instead.See also
Related questions
On sorting
Liston various criteria:On
ComparatorandComparable