Which is the quickest way in Java to sort a list
List<MyObject> myObjectList
based on some numerical property of MyObject?
Which is the quickest way in Java to sort a list
List<MyObject> myObjectList
based on some numerical property of MyObject?
Call
Collections.sort(myObjectList, comparator);
where comparator
is a reference to an instance of Comparator<MyObject>
which compares any two instances of MyObject
appropriately.
Alternatively, if this is a "natural" sort order for MyObject
, you could make MyObject
implement Comparable<MyObject>
and just call
Collections.sort(myObjectList);