views:

68

answers:

1

Which is the quickest way in Java to sort a list

List<MyObject> myObjectList

based on some numerical property of MyObject?

+6  A: 

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);
Jon Skeet
wow! that was quick - many thanks!
davek