package rebelsky.student; import java.util.Comparator; /** * A mechanism for comparing students to each other. The * student who comes "alphabetically first" is considered * to come first. * * @author Samuel A. Rebelsky * @version 1.0 of October 2004. */ public class CompareStudentsByName implements Comparator { /** * Determine if alpha comes alphabetically before beta. * One student comes alphabetically before another student * if (a) the last name of the student is alphabetically * smaller or (b) the last names are the same and the first * name of the student is smaller. * * @param alpha a Student to be compared * @param beta a Student to be compared * @return * (a) a negative number if alpha comes before beta; * (b) a positive number if alpha comes after beta; * (c) zero, if the two have the same first and last names * @exception ClassCastException * if either parameter is not a student */ public int compare(Object alpha, Object beta) { // Convert the two values to students. Student sa = (Student) alpha; Student sb = (Student) beta; // Get last names and use string comparison return sa.getName().compareTo(sb.getName()); } // compare(Object, Object) } // CompareStudentsByName