ComparatorTest.java


import java.util.*;

public class ComparatorTest
{
   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));
      Comparator comp = new CountryComparatorByName();
      Collections.sort(countries, comp); 
         // now the array list is sorted by area
      for (int i = 0; i < countries.size(); i++)
      {
         Country c = (Country) countries.get(i);
         System.out.println(c.getName() + " " + c.getArea());
      }
   }
}


Results

>java ComparatorTest
Belgium 30510.0
Thailand 514000.0
Uruguay 176220.0


Maintained by John Loomis, updated Thu Feb 22 23:37:57 2007