import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;

/**
 * Driver class to demonstrate the use of a Comparator.
 *   @author Dave Reed
 *   @version 10/1/24
 */
public class PersonDriver {
	public static void main(String[] args) {
	    ArrayList<Person> people = new ArrayList<Person>();
		people.add(new Person("Jane", "Doe", 22));
		people.add(new Person("Joe", "College", 20));
		people.add(new Person("Buck", "Doe", 19));
		people.add(new Person("Jane", "Doe", 24));
		  
		TreeSet<Person> peopleQ = new TreeSet<Person>();
		peopleQ.addAll(people);
		System.out.println(peopleQ);

		Collections.sort(people);
		System.out.println(people);
		  
		TreeSet<Person> ageQ = new TreeSet<Person>(youngestFirst);
		ageQ.addAll(people);
        System.out.println(ageQ);

	    Collections.sort(people, youngestFirst);
	    System.out.println(people); 
	}
	  
	///////////////////////////////////////////////////////////////////////////////
	  
	public static Comparator<Person> youngestFirst = new AgeComp();  
	
	private static class AgeComp implements Comparator<Person> {
        public int compare(Person p1, Person p2) {
      	    if (p1.getAge() != p2.getAge()) {
      		    return p1.getAge() - p2.getAge();
      	    }
      	    else if (!p1.getLast().equals(p2.getLast())) {
	            return p1.getLast().compareTo(p2.getLast());
    	    }
    	    else {
    		    return p1.getFirst().compareTo(p2.getFirst());
    	    }
        }
    }
}
