ComparatorTest2.javaThis version uses an anonymous class.
import java.util.*;
public class ComparatorTest2
{
public static void main(String[] args)
{
ArrayList countries = new ArrayList();
countries.add(new Country("Uruguay", 176220));
countries.add(new Country("Thailand", 514000));
countries.add(new Country("Belgium", 30510));
Collections.sort(countries, new
Comparator()
{
public int compare(Object object1, Object object2)
{
Country country1 = (Country) object1;
Country country2 = (Country) object2;
return country1.getName()
.compareTo(country2.getName());
}
});
// now the array list is sorted by name
for (int i = 0; i < countries.size(); i++)
{
Country c = (Country) countries.get(i);
System.out.println(c.getName() + " " + c.getArea());
}
}
}
Maintained by John Loomis, updated Fri Feb 23 17:51:03 2007