SortBy.java


Title:No matter how many different fields to sortby, we only need a single comparator. There is no need to use inner or nested classes. It is simple and straight forward.
Author:Roseanne Zhang

import java.util.*;

class Customer {
   int custId;
   String firstName = new String();
   String lastName = new String();
   String middleName = new String();
   Customer(int custId, String lastName, String firstName, String middleName) {
      this.custId = custId;
      this.lastName = lastName;
      this.firstName = firstName;
      this.middleName = middleName;
   }
}

public class SortBy {

   public static void main(String[] args) {
      SortBy sortBy = new SortBy();
      int    i = 0;

      Customer[] customers = {
         new Customer( 3, "Smith",     "Jake", "B"),
         new Customer( 8, "Hancock",   "John", "J"),
         new Customer( 9, "Jefferson", "Tom",  "R"),
         new Customer( 2, "Johnson",   "Burt", "A"),
         new Customer( 4, "Smith",     "Jake", "J"),
         new Customer( 7, "Marsh",     "Bob",  "W"),
         new Customer( 5, "Brown",     "Jack", "C"),
         new Customer( 1, "Johnson",   "John", "J"),
         new Customer( 6, "Russell",   "Don",  "E"),
         new Customer(10, "Adams",     "John", "Q")
      };

      System.out.println("\nOrder: Random");
      for (i = 0; i < customers.length; i++) {
         System.out.print("ID=" + customers[i].custId);
         System.out.println(" Name=" + customers[i].lastName + ", " +
            customers[i].firstName + " " + customers[i].middleName + ".");
      }

      CompareCustomers c = new CompareCustomers();
      
      System.out.println("\nOrder: CustId");
      
      // SortBy is an attribute of the comparator
      c.setSortBy(CompareCustomers.BY_NAME);

      Arrays.sort(customers, c);
      for (i = 0; i < customers.length; i++) {
         System.out.print("ID=" + customers[i].custId);
         System.out.println(" Name=" + customers[i].lastName + ", " +
            customers[i].firstName + " " + customers[i].middleName + ".");
      }

      System.out.println("\nOrder: Name");
      
      // SortBy is an attribute of the comparator
      c.setSortBy(CompareCustomers.BY_ID);
      
      Arrays.sort(customers, c);
      for (i = 0; i < customers.length; i++) {
         System.out.print("ID=" + customers[i].custId);
         System.out.println(" Name=" + customers[i].lastName + ", " +
            customers[i].firstName + " " + customers[i].middleName + ".");
      }
   }
}

class CompareCustomers implements Comparator {
   final static int BY_NAME = 1;
   final static int BY_ID   = 2;
   // you may have a lot more!!!
   
   private int sortBy = BY_NAME; // default
    
   final private int compareCustId(Customer cust1, Customer cust2) {
      return (cust1.custId - cust2.custId);
   }
   
   final private int compareName(Customer cust1, Customer cust2) {
      int comp = cust1.lastName.compareTo(cust2.lastName);
      if (comp == 0)
         comp = cust1.firstName.compareTo(cust2.firstName);
      if (comp == 0)
         comp = cust1.middleName.compareTo(cust2.middleName);
      return comp;
   }
   
   public void setSortBy(int sortBy) {
      // if caller passed wrong argument, sort by name
      if (sortBy != BY_NAME && sortBy != BY_ID)
         this.sortBy = BY_NAME;
      else
         this.sortBy = sortBy;
   }

   public int compare (Object o1, Object o2) {
      try{
         if (!(o1 instanceof Customer) || !(o2 instanceof Customer)) {
            throw new Exception("CompareCustomers only compares Customer Type");
         }
      }
      catch (Exception e) {
         System.out.println(e);
         System.exit(1);
      }
      
      //Let's use our specific object types.
      Customer cust1 = (Customer) o1;
      Customer cust2 = (Customer) o2;
      
      int comp = 0;
      switch (sortBy) {
         case BY_NAME:
            comp = compareName(cust1,cust2); 
            if (comp != 0)
               break;
            // else fall through
               
         case BY_ID:
            comp = compareCustId(cust1,cust2);
      }
      return comp;
   }
   
   public boolean equals(Object o1, Object o2) {
      return (compare(o1,o2)==0);
   }
}

[join us] [home] [javachina]
Last updated 03-25-2001 by webmaster
Hosted by www.Geocities.ws

1