davek
davek

Reputation: 22915

Sorting List<MyObject>

Which is the quickest way in Java to sort a list

List<MyObject> myObjectList

based on some numerical property of MyObject?

Upvotes: 1

Views: 217

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500275

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);

Upvotes: 6

Related Questions