View Single Post
Old Nov 25th, 2005, 2:22 PM   #2
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
You need a custom Comparator to tell Java how to sort. Comparator objects have a "compare" method that takes two Objects as arguments and returns an integer. If the integer is negative, the first object is less than the second; if the integer is positive, the first object is greater than the second; if the integer is 0, the objects are equal. The String.compareTo method can compare two strings in this way.

You can use an anonymous class to save cluttering up your code with a class that will only be used once. Maybe something like:

Collections.sort(cInd, new Comparator() {
   int compare(Person a, Person b) {
      return a.getLastName().compareTo(b.getLastName());
   }
});
Arevos is offline   Reply With Quote