/**
*  Demonstrates using the SortInterface interface to sort vectors of
*  any object type.
*/

import com.pisoftware.quicksort.*;
import com.pisoftware.quicksort.event.*;

import java.util.*;

public class Example1 {

  // By implementing the SortInterface, objects can be sorted by
  // quickSort reguardless of their other contents.
  static class Record implements SortInterface {

    public String surname;
    public String firstname;
    public Date date_of_birth;

    // Constructs a Record object
    public Record(String firstname, String surname, Date dob) {

      this.firstname = firstname;
      this.surname = surname;
      this.date_of_birth = dob;

    }

    // Print out the record in human readable form
    public String toString() {

     return " Date of Birth: "+date_of_birth+" - "+surname+
       ", "+firstname+".";

    }

    // Here we provide an implementation of the sort method.
    // (This comes from the SortInterface interface)

    public int compareTo(Object sort_with, Object params) {

      // The first parameter will always be of this object type
      Record other = (Record) sort_with;

      // We can do the comparison based on any of the public variables
      // defined on this class.   For this sort, we wish to sort in order
      // of their date of births.

      if (date_of_birth.getTime() == other.date_of_birth.getTime()) {

        // Return 0 if the two objects are the same
        return 0;

      } else if (date_of_birth.getTime() > other.date_of_birth.getTime()) {

        // Return 1 if this object is greater than the other
        return 1;

      } else {

        // Return -1 if this object is smaller than the other
        return -1;

      }

    }

  }


  /**
  *    Prints out the contents of a Vector, in its current order.
  */
  public static void printOrder(Vector vector) {

    for(int i=0; i<vector.size(); i++) {
      System.out.println("  "+vector.elementAt(i).toString());
    }

  }

  /**
  *    Start the example.
  */
  public static void main(String[] args) {

    // First, create the Vector of Record objects we want to sort.

    // The number used in the "new Date()" section is the number of 
    // seconds since the start of 1970.

    Vector records = new Vector();
    records.addElement(new Record("John", "Smith", new Date(334234234)));
    records.addElement(new Record("Wayne", "Edwards", new Date(384859898)));
    records.addElement(new Record("Mary", "Smith", new Date(12121324)));
    records.addElement(new Record("Fred", "Kennedy", new Date(553421)));
    records.addElement(new Record("Joe", "Caveman", new Date(12231)));
    records.addElement(new Record("Elvis", "Presto", new Date(123889900)));
    records.addElement(new Record("Chris", "Ryan", new Date(469989898)));
    records.addElement(new Record("Michael", "Henderson", new Date(453465367)));
    records.addElement(new Record("Brad", "Marshall", new Date(234676576)));
    records.addElement(new Record("David", "Wood", new Date(454545767)));

    System.out.println("Current order of the Vector:");
    printOrder(records);

    // Now, because we know that each element of the Vector implements
    // SortInterface, (See the class definition of Record above), we can
    // pass it to the QuickSort routines and know that when it returns
    // the Vector will be sorted.

    // The Sort is done by asking the Record object to compare itself
    // with another Record object (see the compareTo() method defined
    // above in the Record Class definition)

    QuickSort.quickSort(records);

    System.out.println("Sorted order of the Vector:");
    printOrder(records);

  }


}