I have a Java collection:
Collection<CustomObject> list = new ArrayList<CustomObject>();
CustomObject
has an id
field now before display list I want to sort this collection by that id
.
Is there any way I could that do that?
I have a Java collection:
Collection<CustomObject> list = new ArrayList<CustomObject>();
CustomObject
has an id
field now before display list I want to sort this collection by that id
.
Is there any way I could that do that?
Implement the Comparable interface on your customObject.
Use sort.
You just have to do this:
(Or use the version below it, as others already said.)
Comparator
is the wayAlso See
You can use java Custom Class for the purpose of sorting.
Use a Comparator:
Additionally, if
CustomObject
implementsComparable
, then just useCollections.sort(list)
With JDK 8 the syntax is much simpler.
Much simplier
Simplest
Obviously the initial code can be used for JDK 8 too.
You can also use: