Reputation: 5733
I have a List of obejcts which looks like this:
List<MyObject> myObjects = ...
and MyObject has a reference to another object MyReferencedObject. I will sort List myObjects concerning a field of MyReferencedObject called sortOrder (Integer). Is there a performant possibility to do this in a performant way?
Upvotes: 1
Views: 801
Reputation: 4586
You can either make it implement Comparable
(especially if such comparison is not one-off and will be reused).
Then just call:
Collections.sort(myObjects);
Or define the order using a lambda expression:
Collections.sort(myObjects, (o1, o2) ->
o1.getReferencedObject().getSortOrder()
.compareTo(o2.getReferencedObject().getSortOrder()));
Upvotes: 1