01: import java.util.*; 02: 03: public class ComparatorTest 04: { 05: public static void main(String[] args) 06: { 07: ArrayList countries = new ArrayList(); 08: countries.add(new Country("Uruguay", 176220)); 09: countries.add(new Country("Thailand", 514000)); 10: countries.add(new Country("Belgium", 30510)); 11: 12: Collections.sort(countries, new 13: Comparator() 14: { 15: public int compare(Object object1, Object object2) 16: { 17: Country country1 = (Country) object1; 18: Country country2 = (Country) object2; 19: return country1.getName() 20: .compareTo(country2.getName()); 21: } 22: }); 23: 24: // now the array list is sorted by name 25: for (int i = 0; i < countries.size(); i++) 26: { 27: Country c = (Country) countries.get(i); 28: System.out.println(c.getName() + " " + c.getArea()); 29: } 30: } 31: } 32: