I think this is a great time to learn the Comparable
interface. You could make a Class that compares by score
and prints its id
when toString()
is called. As mentioned earlier, using a custom Comparator
would also suffice, but if you've never worked with Comparable
I recommend learning that first.
Here's a link to the JavaDoc: http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Comparable.html
You should know that for two objects o1 and o2 of type Comparable<T>
, o1.compareTo(o2)
will return:
- -1 if o1 < o2 (in the ordering defined by T, the type of o1 and o2)
- 0 if o1 == o2 (again, in the ordering, not necessarily object equality)
- 1 if o1 > o2
This information will help you write your compareTo
function in your custom class.
Once you have your class written, then Java's Collections
class provides a sort
method that will sort a List
of Comparables
. Easy!
Here's the link for that: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#sort%28java.util.List%29