Source of ComparatorTester.java


  1: import java.util.*;

  3: public class ComparatorTester
  4: {
  5:    public static void main(String[] args)
  6:    {
  7:       ArrayList<Country> countries = new ArrayList<Country>();
  8:       countries.add(new Country("Uruguay", 176220));
  9:       countries.add(new Country("Thailand", 514000));
 10:       countries.add(new Country("Belgium", 30510));

 12:       Collections.sort(countries, new
 13:          Comparator<Country>()
 14:          {
 15:             public int compare(Country country1, Country country2)
 16:             {
 17:                return country1.getName()
 18:                   .compareTo(country2.getName());
 19:             }
 20:          });

 22:       // Now the array list is sorted by name
 23:       for (Country c : countries)
 24:          System.out.println(c.getName() + " " + c.getArea());
 25:    }
 26: }