import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.List; /** * An exercise to demonstrate using a Comparator with the * sort method of the Collections class. * Students sort Employee objects by: * -- using their natural ordering * -- using several other sort orders, with Comparator objects through: * -- a separate top-level class * -- a static nested class * -- a local class inside a method * -- an anonymous class * * @author David Mutchler and YOUR-NAME-HERE, based on work by Matt Boutell. * Created September 30, 2008. */ public class Main { /** * The main method, where execution begins. * It adds Employee objects to a list * and calls several methods for sorting that list. * (Students will implement those methods -- they begin as stubs.) * * @param args Command-line arguments (ignored here) */ public static void main(String[] args) { List employees = new ArrayList(); Main.addEmployees(employees); Collections.shuffle(employees); Main.sortUsingNaturalOrdering(employees); System.out.println(employees); Collections.shuffle(employees); Main.sortUsingTopLevelClassAsComparator(employees); // System.out.println(employees); Collections.shuffle(employees); Main.sortUsingStaticNestedClassAsComparator(employees); // System.out.println(employees); Collections.shuffle(employees); Main.sortUsingLocalClassAsComparator(employees); // System.out.println(employees); Collections.shuffle(employees); Main.sortUsingAnonymousClassAsComparator1(employees); // System.out.println(employees); Collections.shuffle(employees); Main.sortUsingAnonymousClassAsComparator2(employees); // System.out.println(employees); Collections.shuffle(employees); Main.sortUsingAnonymousClassAsComparator3(employees); // System.out.println(employees); } /** * Add some employees to the given list. * * @param employees List to which to add employees */ private static void addEmployees(List employees) { employees.add(new Employee(20, "Bacall", "Lauren", new Date(100L), "Actress")); employees.add(new Employee(43, "Couric", "Katie", new Date(5000000L), "News anchor")); employees.add(new Employee(14, "Bogart", "Humphrey", new Date(200L), "Actor")); employees.add(new Employee(25, "Mantle", "Mickey", new Date(1000L), "Right fielder")); employees.add(new Employee(39, "Madonna", "", new Date(3000000L), "Pop star")); employees.add(new Employee(16, "Bell", "Joshua", new Date(4000000L), "Violinist")); } /** * Sort the given list by using the natural ordering of its elements. * * @param employees List to sort */ private static void sortUsingNaturalOrdering(List employees) { // TODO Auto-generated method stub. } /** * Sort the given list by using a top-level (i.e. NOT nested) class * as the Comparator. You can name the class whatever you like. * Sort by the Employee's last name. * * @param employees List to sort */ private static void sortUsingTopLevelClassAsComparator(List employees) { // TODO Auto-generated method stub. } /** * Sort the given list by using a static nested class as the Comparator. * The class should be nested in this class. Name it whatever you like. * Sort by the Employee's last name. * * @param employees List to sort */ private static void sortUsingStaticNestedClassAsComparator(List employees) { // TODO Auto-generated method stub. } /** * Sort the given list by using a local class as the Comparator. * The local class should be inside this method. Name it whatever you like. * Sort by the Employee's last name. * * @param employees List to sort */ private static void sortUsingLocalClassAsComparator(List employees) { // TODO Auto-generated method stub. } /** * Sort the given list by using an anonymous class as the Comparator. * Sort by the Employee's last name. * * @param employees List to sort */ private static void sortUsingAnonymousClassAsComparator1(List employees) { // TODO Auto-generated method stub. } /** * Sort the given list by using an anonymous class as the Comparator. * Sort by the Employee's date of employment, * from oldest hires to most recent hires. * * @param employees List to sort */ private static void sortUsingAnonymousClassAsComparator2(List employees) { // TODO Auto-generated method stub. } /** * Sort the given list by using an anonymous class as the Comparator. * Sort by the Employee's date of employment, * from most recent hires to oldest hires. * * @param employees List to sort */ private static void sortUsingAnonymousClassAsComparator3(List employees) { // TODO Auto-generated method stub. } }