4 Replies - 546 Views - Last Post: 21 February 2010 - 01:17 PM Rate Topic: -----

#1 AmateurC  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 71
  • Joined: 12-June 09

Name and birth date sort

Posted 21 February 2010 - 12:18 PM

I wrote a program that sorts the first name, last name, and birth date. The program sorts the last name in a array; but if the last name is the same, it sorts by first name. However, if the first name is the same, then it sorts by birth date from oldest to youngest. However when the program compared the birth date it did not sort birth date correctly. What is wrong with my code?
import java.util.Arrays;

/**
 * 
 * @author javadb.com
 */
class Main2exercise3 {

    /**
         * Creates objects using the Fruit class which implements the Comparable
         * interface. Compares and sorts the objects by mileage.
         */
    public void comparableExample() {

        // Creating the objects that implements the Comparable interface
         Employee[] workers = new Employee[18];
         workers[0]  = new Employee( "Charles", "Adams",  1963);
         workers[1]  = new Employee( "Donald",  "Adams",  1959);
         workers[2]  = new Employee( "Elmer",   "Adams",  1976);
         workers[3]  = new Employee( "Nancy",   "Devon",  1963);
         workers[4]  = new Employee( "Andrew",  "Lewis",  1983);
         workers[5]  = new Employee( "Douglas", "Page",   1981);
         workers[6]  = new Employee( "Donald",  "Wolder", 1963);
         workers[7]  = new Employee( "Henry",   "Wolder", 1972);
         workers[8]  = new Employee( "Robert",  "Wolder", 1959);
         workers[9]  = new Employee( "Howard",  "Cohen",  1984);
         workers[10] = new Employee( "Howard",  "Cohen",  1933);
         workers[11] = new Employee( "Howard",  "Cohen",  1958);
         workers[12] = new Employee( "Donald",  "Rice",   1935);
         workers[13] = new Employee( "Aaron",   "Adams",  1935);
         workers[14] = new Employee( "Bob",     "Adams",  1935);
         workers[15] = new Employee( "Bob",     "Adamson",1970);
         workers[16] = new Employee( "Susan",   "Adamson",1934);
         workers[17] = new Employee( "Sally",   "Adamson",1968);
     
        // Comparing the objects by calling the compareTo method on one of them
        // passing another object as argument.

        for (int i = 0; i < workers.length; i++) 
        {
            for (int j = 0; j < workers.length; j++)
            {
                System.out.println("Worker " + (i + 1) + " equals worker " + (j + 1) + ": " + workers[i].compareTo(workers[j]));
               
            }
        }

        Arrays.sort(workers);
        for (int o = 0; o < workers.length; o++)
        {
            System.out.println(workers[o].toString());
        }
    }

    class Employee implements Comparable {

       private String firstName;
       private String lastName;
       private int birthYear;

       public Employee(String firstName, String lastName, int birthYear) {

            this.firstName = firstName;
            this.lastName = lastName;
            this.birthYear = birthYear;

        }

        public int compareTo(Object obj)
        {
          if (obj instanceof Employee)
            {
                Employee worker = (Employee) obj;
                if ((this.getLastName().compareToIgnoreCase(worker.getLastName())) > 0)
                    return 1;
                else if ((this.getLastName().compareToIgnoreCase(worker.getLastName())) < 0)
                    return -1;
            }
            else if (obj instanceof Employee)
            {
              Employee worker = (Employee) obj;
               if ((this.getFirstName().compareToIgnoreCase(worker.getFirstName())) > 0)
                    return 1;
                else if ((this.getFirstName().compareToIgnoreCase(worker.getFirstName())) < 0)
                    return -1;
            }
            else
            {
                Employee worker = (Employee) obj;
                if (this.getBirthYear() > worker.getBirthYear())
                    return 1;
                else if (this.getBirthYear() < worker.getBirthYear())
                   return -1; 
           } 
            return 0;
        }
       
       public void setfirstName(String firstName) {
            this.firstName = firstName;
        }

        public void setWeight(String lastName) {
            this.lastName = lastName;
        }

        public void setTaste(int birthYear) {
            this.birthYear = birthYear;
        }

        public String getFirstName() {
            return firstName;
        }

       public String getLastName() {
            return lastName;
        }
        public int getBirthYear() {
            return birthYear;

        }

        public String toString() {

            return "First name: " + firstName + "      \tLast name: " + lastName + "      \tYear of birth: " + birthYear;
        }
    }
  
    /**
     * @param args the command line arguments
     */
   
    public static void main(String[] args) {
        new Main2exercise3().comparableExample();
    }
}



The output is this:
Worker 1 equals worker 1: 0
Worker 1 equals worker 2: 0
Worker 1 equals worker 3: 0
Worker 1 equals worker 4: -1
Worker 1 equals worker 5: -1
Worker 1 equals worker 6: -1
Worker 1 equals worker 7: -1
Worker 1 equals worker 8: -1
Worker 1 equals worker 9: -1
Worker 1 equals worker 10: -1
Worker 1 equals worker 11: -1
Worker 1 equals worker 12: -1
Worker 1 equals worker 13: -1
Worker 1 equals worker 14: 0
Worker 1 equals worker 15: 0
Worker 1 equals worker 16: -1
Worker 1 equals worker 17: -1
Worker 1 equals worker 18: -1
Worker 2 equals worker 1: 0
Worker 2 equals worker 2: 0
Worker 2 equals worker 3: 0
Worker 2 equals worker 4: -1
Worker 2 equals worker 5: -1
Worker 2 equals worker 6: -1
Worker 2 equals worker 7: -1
Worker 2 equals worker 8: -1
Worker 2 equals worker 9: -1
Worker 2 equals worker 10: -1
Worker 2 equals worker 11: -1
Worker 2 equals worker 12: -1
Worker 2 equals worker 13: -1
Worker 2 equals worker 14: 0
Worker 2 equals worker 15: 0
Worker 2 equals worker 16: -1
Worker 2 equals worker 17: -1
Worker 2 equals worker 18: -1
Worker 3 equals worker 1: 0
Worker 3 equals worker 2: 0
Worker 3 equals worker 3: 0
Worker 3 equals worker 4: -1
Worker 3 equals worker 5: -1
Worker 3 equals worker 6: -1
Worker 3 equals worker 7: -1
Worker 3 equals worker 8: -1
Worker 3 equals worker 9: -1
Worker 3 equals worker 10: -1
Worker 3 equals worker 11: -1
Worker 3 equals worker 12: -1
Worker 3 equals worker 13: -1
Worker 3 equals worker 14: 0
Worker 3 equals worker 15: 0
Worker 3 equals worker 16: -1
Worker 3 equals worker 17: -1
Worker 3 equals worker 18: -1
Worker 4 equals worker 1: 1
Worker 4 equals worker 2: 1
Worker 4 equals worker 3: 1
Worker 4 equals worker 4: 0
Worker 4 equals worker 5: -1
Worker 4 equals worker 6: -1
Worker 4 equals worker 7: -1
Worker 4 equals worker 8: -1
Worker 4 equals worker 9: -1
Worker 4 equals worker 10: 1
Worker 4 equals worker 11: 1
Worker 4 equals worker 12: 1
Worker 4 equals worker 13: -1
Worker 4 equals worker 14: 1
Worker 4 equals worker 15: 1
Worker 4 equals worker 16: 1
Worker 4 equals worker 17: 1
Worker 4 equals worker 18: 1
Worker 5 equals worker 1: 1
Worker 5 equals worker 2: 1
Worker 5 equals worker 3: 1
Worker 5 equals worker 4: 1
Worker 5 equals worker 5: 0
Worker 5 equals worker 6: -1
Worker 5 equals worker 7: -1
Worker 5 equals worker 8: -1
Worker 5 equals worker 9: -1
Worker 5 equals worker 10: 1
Worker 5 equals worker 11: 1
Worker 5 equals worker 12: 1
Worker 5 equals worker 13: -1
Worker 5 equals worker 14: 1
Worker 5 equals worker 15: 1
Worker 5 equals worker 16: 1
Worker 5 equals worker 17: 1
Worker 5 equals worker 18: 1
Worker 6 equals worker 1: 1
Worker 6 equals worker 2: 1
Worker 6 equals worker 3: 1
Worker 6 equals worker 4: 1
Worker 6 equals worker 5: 1
Worker 6 equals worker 6: 0
Worker 6 equals worker 7: -1
Worker 6 equals worker 8: -1
Worker 6 equals worker 9: -1
Worker 6 equals worker 10: 1
Worker 6 equals worker 11: 1
Worker 6 equals worker 12: 1
Worker 6 equals worker 13: -1
Worker 6 equals worker 14: 1
Worker 6 equals worker 15: 1
Worker 6 equals worker 16: 1
Worker 6 equals worker 17: 1
Worker 6 equals worker 18: 1
Worker 7 equals worker 1: 1
Worker 7 equals worker 2: 1
Worker 7 equals worker 3: 1
Worker 7 equals worker 4: 1
Worker 7 equals worker 5: 1
Worker 7 equals worker 6: 1
Worker 7 equals worker 7: 0
Worker 7 equals worker 8: 0
Worker 7 equals worker 9: 0
Worker 7 equals worker 10: 1
Worker 7 equals worker 11: 1
Worker 7 equals worker 12: 1
Worker 7 equals worker 13: 1
Worker 7 equals worker 14: 1
Worker 7 equals worker 15: 1
Worker 7 equals worker 16: 1
Worker 7 equals worker 17: 1
Worker 7 equals worker 18: 1
Worker 8 equals worker 1: 1
Worker 8 equals worker 2: 1
Worker 8 equals worker 3: 1
Worker 8 equals worker 4: 1
Worker 8 equals worker 5: 1
Worker 8 equals worker 6: 1
Worker 8 equals worker 7: 0
Worker 8 equals worker 8: 0
Worker 8 equals worker 9: 0
Worker 8 equals worker 10: 1
Worker 8 equals worker 11: 1
Worker 8 equals worker 12: 1
Worker 8 equals worker 13: 1
Worker 8 equals worker 14: 1
Worker 8 equals worker 15: 1
Worker 8 equals worker 16: 1
Worker 8 equals worker 17: 1
Worker 8 equals worker 18: 1
Worker 9 equals worker 1: 1
Worker 9 equals worker 2: 1
Worker 9 equals worker 3: 1
Worker 9 equals worker 4: 1
Worker 9 equals worker 5: 1
Worker 9 equals worker 6: 1
Worker 9 equals worker 7: 0
Worker 9 equals worker 8: 0
Worker 9 equals worker 9: 0
Worker 9 equals worker 10: 1
Worker 9 equals worker 11: 1
Worker 9 equals worker 12: 1
Worker 9 equals worker 13: 1
Worker 9 equals worker 14: 1
Worker 9 equals worker 15: 1
Worker 9 equals worker 16: 1
Worker 9 equals worker 17: 1
Worker 9 equals worker 18: 1
Worker 10 equals worker 1: 1
Worker 10 equals worker 2: 1
Worker 10 equals worker 3: 1
Worker 10 equals worker 4: -1
Worker 10 equals worker 5: -1
Worker 10 equals worker 6: -1
Worker 10 equals worker 7: -1
Worker 10 equals worker 8: -1
Worker 10 equals worker 9: -1
Worker 10 equals worker 10: 0
Worker 10 equals worker 11: 0
Worker 10 equals worker 12: 0
Worker 10 equals worker 13: -1
Worker 10 equals worker 14: 1
Worker 10 equals worker 15: 1
Worker 10 equals worker 16: 1
Worker 10 equals worker 17: 1
Worker 10 equals worker 18: 1
Worker 11 equals worker 1: 1
Worker 11 equals worker 2: 1
Worker 11 equals worker 3: 1
Worker 11 equals worker 4: -1
Worker 11 equals worker 5: -1
Worker 11 equals worker 6: -1
Worker 11 equals worker 7: -1
Worker 11 equals worker 8: -1
Worker 11 equals worker 9: -1
Worker 11 equals worker 10: 0
Worker 11 equals worker 11: 0
Worker 11 equals worker 12: 0
Worker 11 equals worker 13: -1
Worker 11 equals worker 14: 1
Worker 11 equals worker 15: 1
Worker 11 equals worker 16: 1
Worker 11 equals worker 17: 1
Worker 11 equals worker 18: 1
Worker 12 equals worker 1: 1
Worker 12 equals worker 2: 1
Worker 12 equals worker 3: 1
Worker 12 equals worker 4: -1
Worker 12 equals worker 5: -1
Worker 12 equals worker 6: -1
Worker 12 equals worker 7: -1
Worker 12 equals worker 8: -1
Worker 12 equals worker 9: -1
Worker 12 equals worker 10: 0
Worker 12 equals worker 11: 0
Worker 12 equals worker 12: 0
Worker 12 equals worker 13: -1
Worker 12 equals worker 14: 1
Worker 12 equals worker 15: 1
Worker 12 equals worker 16: 1
Worker 12 equals worker 17: 1
Worker 12 equals worker 18: 1
Worker 13 equals worker 1: 1
Worker 13 equals worker 2: 1
Worker 13 equals worker 3: 1
Worker 13 equals worker 4: 1
Worker 13 equals worker 5: 1
Worker 13 equals worker 6: 1
Worker 13 equals worker 7: -1
Worker 13 equals worker 8: -1
Worker 13 equals worker 9: -1
Worker 13 equals worker 10: 1
Worker 13 equals worker 11: 1
Worker 13 equals worker 12: 1
Worker 13 equals worker 13: 0
Worker 13 equals worker 14: 1
Worker 13 equals worker 15: 1
Worker 13 equals worker 16: 1
Worker 13 equals worker 17: 1
Worker 13 equals worker 18: 1
Worker 14 equals worker 1: 0
Worker 14 equals worker 2: 0
Worker 14 equals worker 3: 0
Worker 14 equals worker 4: -1
Worker 14 equals worker 5: -1
Worker 14 equals worker 6: -1
Worker 14 equals worker 7: -1
Worker 14 equals worker 8: -1
Worker 14 equals worker 9: -1
Worker 14 equals worker 10: -1
Worker 14 equals worker 11: -1
Worker 14 equals worker 12: -1
Worker 14 equals worker 13: -1
Worker 14 equals worker 14: 0
Worker 14 equals worker 15: 0
Worker 14 equals worker 16: -1
Worker 14 equals worker 17: -1
Worker 14 equals worker 18: -1
Worker 15 equals worker 1: 0
Worker 15 equals worker 2: 0
Worker 15 equals worker 3: 0
Worker 15 equals worker 4: -1
Worker 15 equals worker 5: -1
Worker 15 equals worker 6: -1
Worker 15 equals worker 7: -1
Worker 15 equals worker 8: -1
Worker 15 equals worker 9: -1
Worker 15 equals worker 10: -1
Worker 15 equals worker 11: -1
Worker 15 equals worker 12: -1
Worker 15 equals worker 13: -1
Worker 15 equals worker 14: 0
Worker 15 equals worker 15: 0
Worker 15 equals worker 16: -1
Worker 15 equals worker 17: -1
Worker 15 equals worker 18: -1
Worker 16 equals worker 1: 1
Worker 16 equals worker 2: 1
Worker 16 equals worker 3: 1
Worker 16 equals worker 4: -1
Worker 16 equals worker 5: -1
Worker 16 equals worker 6: -1
Worker 16 equals worker 7: -1
Worker 16 equals worker 8: -1
Worker 16 equals worker 9: -1
Worker 16 equals worker 10: -1
Worker 16 equals worker 11: -1
Worker 16 equals worker 12: -1
Worker 16 equals worker 13: -1
Worker 16 equals worker 14: 1
Worker 16 equals worker 15: 1
Worker 16 equals worker 16: 0
Worker 16 equals worker 17: 0
Worker 16 equals worker 18: 0
Worker 17 equals worker 1: 1
Worker 17 equals worker 2: 1
Worker 17 equals worker 3: 1
Worker 17 equals worker 4: -1
Worker 17 equals worker 5: -1
Worker 17 equals worker 6: -1
Worker 17 equals worker 7: -1
Worker 17 equals worker 8: -1
Worker 17 equals worker 9: -1
Worker 17 equals worker 10: -1
Worker 17 equals worker 11: -1
Worker 17 equals worker 12: -1
Worker 17 equals worker 13: -1
Worker 17 equals worker 14: 1
Worker 17 equals worker 15: 1
Worker 17 equals worker 16: 0
Worker 17 equals worker 17: 0
Worker 17 equals worker 18: 0
Worker 18 equals worker 1: 1
Worker 18 equals worker 2: 1
Worker 18 equals worker 3: 1
Worker 18 equals worker 4: -1
Worker 18 equals worker 5: -1
Worker 18 equals worker 6: -1
Worker 18 equals worker 7: -1
Worker 18 equals worker 8: -1
Worker 18 equals worker 9: -1
Worker 18 equals worker 10: -1
Worker 18 equals worker 11: -1
Worker 18 equals worker 12: -1
Worker 18 equals worker 13: -1
Worker 18 equals worker 14: 1
Worker 18 equals worker 15: 1
Worker 18 equals worker 16: 0
Worker 18 equals worker 17: 0
Worker 18 equals worker 18: 0
First name: Charles      	Last name: Adams      	Year of birth: 1963
First name: Donald      	Last name: Adams      	Year of birth: 1959
First name: Elmer      	Last name: Adams      	Year of birth: 1976
First name: Aaron      	Last name: Adams      	Year of birth: 1935
First name: Bob      	Last name: Adams      	Year of birth: 1935
First name: Bob      	Last name: Adamson      	Year of birth: 1970
First name: Susan      	Last name: Adamson      	Year of birth: 1934
First name: Sally      	Last name: Adamson      	Year of birth: 1968
First name: Howard      	Last name: Cohen      	Year of birth: 1984
First name: Howard      	Last name: Cohen      	Year of birth: 1933
First name: Howard      	Last name: Cohen      	Year of birth: 1958
First name: Nancy      	Last name: Devon      	Year of birth: 1963
First name: Andrew      	Last name: Lewis      	Year of birth: 1983
First name: Douglas      	Last name: Page      	Year of birth: 1981
First name: Donald      	Last name: Rice      	Year of birth: 1935
First name: Donald      	Last name: Wolder      	Year of birth: 1963
First name: Henry      	Last name: Wolder      	Year of birth: 1972
First name: Robert      	Last name: Wolder      	Year of birth: 1959


This post has been edited by AmateurC: 21 February 2010 - 12:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Name and birth date sort

#2 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Name and birth date sort

Posted 21 February 2010 - 12:24 PM

if (obj instanceof Employee)
            {
                Employee worker = (Employee) obj;
                if ((this.getLastName().compareToIgnoreCase(worker.getLastName())) > 0)
                    return 1;
                else if ((this.getLastName().compareToIgnoreCase(worker.getLastName())) < 0)
                    return -1;
            }
            else if (obj instanceof Employee)
            {
              Employee worker = (Employee) obj;
               if ((this.getFirstName().compareToIgnoreCase(worker.getFirstName())) > 0)
                    return 1;
                else if ((this.getFirstName().compareToIgnoreCase(worker.getFirstName())) < 0)
                    return -1;
            }


If the first if is false, the else if will be false as well, and it will go straight to the else, since the if and else if both test the exact same condition.
Was This Post Helpful? 1
  • +
  • -

#3 AmateurC  Icon User is offline

  • D.I.C Head

Reputation: -5
  • View blog
  • Posts: 71
  • Joined: 12-June 09

Re: Name and birth date sort

Posted 21 February 2010 - 12:43 PM

View Posterik.price, on 21 February 2010 - 11:24 AM, said:

if (obj instanceof Employee)
            {
                Employee worker = (Employee) obj;
                if ((this.getLastName().compareToIgnoreCase(worker.getLastName())) > 0)
                    return 1;
                else if ((this.getLastName().compareToIgnoreCase(worker.getLastName())) < 0)
                    return -1;
            }
            else if (obj instanceof Employee)
            {
              Employee worker = (Employee) obj;
               if ((this.getFirstName().compareToIgnoreCase(worker.getFirstName())) > 0)
                    return 1;
                else if ((this.getFirstName().compareToIgnoreCase(worker.getFirstName())) < 0)
                    return -1;
            }


If the first if is false, the else if will be false as well, and it will go straight to the else, since the if and else if both test the exact same condition.

When I put your code into mine, I added "return 0" because the compiler required a return statement.
       if (obj instanceof Employee)
            {
                Employee worker = (Employee) obj;
                if ((this.getLastName().compareToIgnoreCase(worker.getLastName())) > 0)
                    return 1;
                else if ((this.getLastName().compareToIgnoreCase(worker.getLastName())) < 0)
                    return -1;
            }
            else if (obj instanceof Employee)
            {
              Employee worker = (Employee) obj;
               if ((this.getFirstName().compareToIgnoreCase(worker.getFirstName())) > 0)
                    return 1;
                else if ((this.getFirstName().compareToIgnoreCase(worker.getFirstName())) < 0)
                    return -1;
            }
          return 0; 



Also, the program still did not work the way it is supposed to do. The output is still the same.

> java Main2exercise3 // This is not code, just the output from the program.
Worker 1 equals worker 1: 0
Worker 1 equals worker 2: 0
Worker 1 equals worker 3: 0
Worker 1 equals worker 4: -1
Worker 1 equals worker 5: -1
Worker 1 equals worker 6: -1
Worker 1 equals worker 7: -1
Worker 1 equals worker 8: -1
Worker 1 equals worker 9: -1
Worker 1 equals worker 10: -1
Worker 1 equals worker 11: -1
Worker 1 equals worker 12: -1
Worker 1 equals worker 13: -1
Worker 1 equals worker 14: 0
Worker 1 equals worker 15: 0
Worker 1 equals worker 16: -1
Worker 1 equals worker 17: -1
Worker 1 equals worker 18: -1
Worker 2 equals worker 1: 0
Worker 2 equals worker 2: 0
Worker 2 equals worker 3: 0
Worker 2 equals worker 4: -1
Worker 2 equals worker 5: -1
Worker 2 equals worker 6: -1
Worker 2 equals worker 7: -1
Worker 2 equals worker 8: -1
Worker 2 equals worker 9: -1
Worker 2 equals worker 10: -1
Worker 2 equals worker 11: -1
Worker 2 equals worker 12: -1
Worker 2 equals worker 13: -1
Worker 2 equals worker 14: 0
Worker 2 equals worker 15: 0
Worker 2 equals worker 16: -1
Worker 2 equals worker 17: -1
Worker 2 equals worker 18: -1
Worker 3 equals worker 1: 0
Worker 3 equals worker 2: 0
Worker 3 equals worker 3: 0
Worker 3 equals worker 4: -1
Worker 3 equals worker 5: -1
Worker 3 equals worker 6: -1
Worker 3 equals worker 7: -1
Worker 3 equals worker 8: -1
Worker 3 equals worker 9: -1
Worker 3 equals worker 10: -1
Worker 3 equals worker 11: -1
Worker 3 equals worker 12: -1
Worker 3 equals worker 13: -1
Worker 3 equals worker 14: 0
Worker 3 equals worker 15: 0
Worker 3 equals worker 16: -1
Worker 3 equals worker 17: -1
Worker 3 equals worker 18: -1
Worker 4 equals worker 1: 1
Worker 4 equals worker 2: 1
Worker 4 equals worker 3: 1
Worker 4 equals worker 4: 0
Worker 4 equals worker 5: -1
Worker 4 equals worker 6: -1
Worker 4 equals worker 7: -1
Worker 4 equals worker 8: -1
Worker 4 equals worker 9: -1
Worker 4 equals worker 10: 1
Worker 4 equals worker 11: 1
Worker 4 equals worker 12: 1
Worker 4 equals worker 13: -1
Worker 4 equals worker 14: 1
Worker 4 equals worker 15: 1
Worker 4 equals worker 16: 1
Worker 4 equals worker 17: 1
Worker 4 equals worker 18: 1
Worker 5 equals worker 1: 1
Worker 5 equals worker 2: 1
Worker 5 equals worker 3: 1
Worker 5 equals worker 4: 1
Worker 5 equals worker 5: 0
Worker 5 equals worker 6: -1
Worker 5 equals worker 7: -1
Worker 5 equals worker 8: -1
Worker 5 equals worker 9: -1
Worker 5 equals worker 10: 1
Worker 5 equals worker 11: 1
Worker 5 equals worker 12: 1
Worker 5 equals worker 13: -1
Worker 5 equals worker 14: 1
Worker 5 equals worker 15: 1
Worker 5 equals worker 16: 1
Worker 5 equals worker 17: 1
Worker 5 equals worker 18: 1
Worker 6 equals worker 1: 1
Worker 6 equals worker 2: 1
Worker 6 equals worker 3: 1
Worker 6 equals worker 4: 1
Worker 6 equals worker 5: 1
Worker 6 equals worker 6: 0
Worker 6 equals worker 7: -1
Worker 6 equals worker 8: -1
Worker 6 equals worker 9: -1
Worker 6 equals worker 10: 1
Worker 6 equals worker 11: 1
Worker 6 equals worker 12: 1
Worker 6 equals worker 13: -1
Worker 6 equals worker 14: 1
Worker 6 equals worker 15: 1
Worker 6 equals worker 16: 1
Worker 6 equals worker 17: 1
Worker 6 equals worker 18: 1
Worker 7 equals worker 1: 1
Worker 7 equals worker 2: 1
Worker 7 equals worker 3: 1
Worker 7 equals worker 4: 1
Worker 7 equals worker 5: 1
Worker 7 equals worker 6: 1
Worker 7 equals worker 7: 0
Worker 7 equals worker 8: 0
Worker 7 equals worker 9: 0
Worker 7 equals worker 10: 1
Worker 7 equals worker 11: 1
Worker 7 equals worker 12: 1
Worker 7 equals worker 13: 1
Worker 7 equals worker 14: 1
Worker 7 equals worker 15: 1
Worker 7 equals worker 16: 1
Worker 7 equals worker 17: 1
Worker 7 equals worker 18: 1
Worker 8 equals worker 1: 1
Worker 8 equals worker 2: 1
Worker 8 equals worker 3: 1
Worker 8 equals worker 4: 1
Worker 8 equals worker 5: 1
Worker 8 equals worker 6: 1
Worker 8 equals worker 7: 0
Worker 8 equals worker 8: 0
Worker 8 equals worker 9: 0
Worker 8 equals worker 10: 1
Worker 8 equals worker 11: 1
Worker 8 equals worker 12: 1
Worker 8 equals worker 13: 1
Worker 8 equals worker 14: 1
Worker 8 equals worker 15: 1
Worker 8 equals worker 16: 1
Worker 8 equals worker 17: 1
Worker 8 equals worker 18: 1
Worker 9 equals worker 1: 1
Worker 9 equals worker 2: 1
Worker 9 equals worker 3: 1
Worker 9 equals worker 4: 1
Worker 9 equals worker 5: 1
Worker 9 equals worker 6: 1
Worker 9 equals worker 7: 0
Worker 9 equals worker 8: 0
Worker 9 equals worker 9: 0
Worker 9 equals worker 10: 1
Worker 9 equals worker 11: 1
Worker 9 equals worker 12: 1
Worker 9 equals worker 13: 1
Worker 9 equals worker 14: 1
Worker 9 equals worker 15: 1
Worker 9 equals worker 16: 1
Worker 9 equals worker 17: 1
Worker 9 equals worker 18: 1
Worker 10 equals worker 1: 1
Worker 10 equals worker 2: 1
Worker 10 equals worker 3: 1
Worker 10 equals worker 4: -1
Worker 10 equals worker 5: -1
Worker 10 equals worker 6: -1
Worker 10 equals worker 7: -1
Worker 10 equals worker 8: -1
Worker 10 equals worker 9: -1
Worker 10 equals worker 10: 0
Worker 10 equals worker 11: 0
Worker 10 equals worker 12: 0
Worker 10 equals worker 13: -1
Worker 10 equals worker 14: 1
Worker 10 equals worker 15: 1
Worker 10 equals worker 16: 1
Worker 10 equals worker 17: 1
Worker 10 equals worker 18: 1
Worker 11 equals worker 1: 1
Worker 11 equals worker 2: 1
Worker 11 equals worker 3: 1
Worker 11 equals worker 4: -1
Worker 11 equals worker 5: -1
Worker 11 equals worker 6: -1
Worker 11 equals worker 7: -1
Worker 11 equals worker 8: -1
Worker 11 equals worker 9: -1
Worker 11 equals worker 10: 0
Worker 11 equals worker 11: 0
Worker 11 equals worker 12: 0
Worker 11 equals worker 13: -1
Worker 11 equals worker 14: 1
Worker 11 equals worker 15: 1
Worker 11 equals worker 16: 1
Worker 11 equals worker 17: 1
Worker 11 equals worker 18: 1
Worker 12 equals worker 1: 1
Worker 12 equals worker 2: 1
Worker 12 equals worker 3: 1
Worker 12 equals worker 4: -1
Worker 12 equals worker 5: -1
Worker 12 equals worker 6: -1
Worker 12 equals worker 7: -1
Worker 12 equals worker 8: -1
Worker 12 equals worker 9: -1
Worker 12 equals worker 10: 0
Worker 12 equals worker 11: 0
Worker 12 equals worker 12: 0
Worker 12 equals worker 13: -1
Worker 12 equals worker 14: 1
Worker 12 equals worker 15: 1
Worker 12 equals worker 16: 1
Worker 12 equals worker 17: 1
Worker 12 equals worker 18: 1
Worker 13 equals worker 1: 1
Worker 13 equals worker 2: 1
Worker 13 equals worker 3: 1
Worker 13 equals worker 4: 1
Worker 13 equals worker 5: 1
Worker 13 equals worker 6: 1
Worker 13 equals worker 7: -1
Worker 13 equals worker 8: -1
Worker 13 equals worker 9: -1
Worker 13 equals worker 10: 1
Worker 13 equals worker 11: 1
Worker 13 equals worker 12: 1
Worker 13 equals worker 13: 0
Worker 13 equals worker 14: 1
Worker 13 equals worker 15: 1
Worker 13 equals worker 16: 1
Worker 13 equals worker 17: 1
Worker 13 equals worker 18: 1
Worker 14 equals worker 1: 0
Worker 14 equals worker 2: 0
Worker 14 equals worker 3: 0
Worker 14 equals worker 4: -1
Worker 14 equals worker 5: -1
Worker 14 equals worker 6: -1
Worker 14 equals worker 7: -1
Worker 14 equals worker 8: -1
Worker 14 equals worker 9: -1
Worker 14 equals worker 10: -1
Worker 14 equals worker 11: -1
Worker 14 equals worker 12: -1
Worker 14 equals worker 13: -1
Worker 14 equals worker 14: 0
Worker 14 equals worker 15: 0
Worker 14 equals worker 16: -1
Worker 14 equals worker 17: -1
Worker 14 equals worker 18: -1
Worker 15 equals worker 1: 0
Worker 15 equals worker 2: 0
Worker 15 equals worker 3: 0
Worker 15 equals worker 4: -1
Worker 15 equals worker 5: -1
Worker 15 equals worker 6: -1
Worker 15 equals worker 7: -1
Worker 15 equals worker 8: -1
Worker 15 equals worker 9: -1
Worker 15 equals worker 10: -1
Worker 15 equals worker 11: -1
Worker 15 equals worker 12: -1
Worker 15 equals worker 13: -1
Worker 15 equals worker 14: 0
Worker 15 equals worker 15: 0
Worker 15 equals worker 16: -1
Worker 15 equals worker 17: -1
Worker 15 equals worker 18: -1
Worker 16 equals worker 1: 1
Worker 16 equals worker 2: 1
Worker 16 equals worker 3: 1
Worker 16 equals worker 4: -1
Worker 16 equals worker 5: -1
Worker 16 equals worker 6: -1
Worker 16 equals worker 7: -1
Worker 16 equals worker 8: -1
Worker 16 equals worker 9: -1
Worker 16 equals worker 10: -1
Worker 16 equals worker 11: -1
Worker 16 equals worker 12: -1
Worker 16 equals worker 13: -1
Worker 16 equals worker 14: 1
Worker 16 equals worker 15: 1
Worker 16 equals worker 16: 0
Worker 16 equals worker 17: 0
Worker 16 equals worker 18: 0
Worker 17 equals worker 1: 1
Worker 17 equals worker 2: 1
Worker 17 equals worker 3: 1
Worker 17 equals worker 4: -1
Worker 17 equals worker 5: -1
Worker 17 equals worker 6: -1
Worker 17 equals worker 7: -1
Worker 17 equals worker 8: -1
Worker 17 equals worker 9: -1
Worker 17 equals worker 10: -1
Worker 17 equals worker 11: -1
Worker 17 equals worker 12: -1
Worker 17 equals worker 13: -1
Worker 17 equals worker 14: 1
Worker 17 equals worker 15: 1
Worker 17 equals worker 16: 0
Worker 17 equals worker 17: 0
Worker 17 equals worker 18: 0
Worker 18 equals worker 1: 1
Worker 18 equals worker 2: 1
Worker 18 equals worker 3: 1
Worker 18 equals worker 4: -1
Worker 18 equals worker 5: -1
Worker 18 equals worker 6: -1
Worker 18 equals worker 7: -1
Worker 18 equals worker 8: -1
Worker 18 equals worker 9: -1
Worker 18 equals worker 10: -1
Worker 18 equals worker 11: -1
Worker 18 equals worker 12: -1
Worker 18 equals worker 13: -1
Worker 18 equals worker 14: 1
Worker 18 equals worker 15: 1
Worker 18 equals worker 16: 0
Worker 18 equals worker 17: 0
Worker 18 equals worker 18: 0
First name: Charles      	Last name: Adams      	Year of birth: 1963
First name: Donald      	Last name: Adams      	Year of birth: 1959
First name: Elmer      	Last name: Adams      	Year of birth: 1976
First name: Aaron      	Last name: Adams      	Year of birth: 1935
First name: Bob      	Last name: Adams      	Year of birth: 1935
First name: Bob      	Last name: Adamson      	Year of birth: 1970
First name: Susan      	Last name: Adamson      	Year of birth: 1934
First name: Sally      	Last name: Adamson      	Year of birth: 1968
First name: Howard      	Last name: Cohen      	Year of birth: 1984   // The name Howard Cohen appears three times, so sorting by birth date from  
                                                                                 oldest to youngest is needed. But the program printed the oldest in the 
                                                                                 middle instead of the first.
First name: Howard      	Last name: Cohen      	Year of birth: 1933
First name: Howard      	Last name: Cohen      	Year of birth: 1958
First name: Nancy      	Last name: Devon      	Year of birth: 1963
First name: Andrew      	Last name: Lewis      	Year of birth: 1983
First name: Douglas      	Last name: Page      	Year of birth: 1981
First name: Donald      	Last name: Rice      	Year of birth: 1935
First name: Donald      	Last name: Wolder      	Year of birth: 1963
First name: Henry      	Last name: Wolder      	Year of birth: 1972
First name: Robert      	Last name: Wolder      	Year of birth: 1959


Was This Post Helpful? 0
  • +
  • -

#4 erik.price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 485
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Re: Name and birth date sort

Posted 21 February 2010 - 12:44 PM

I didn't make any changes. I just highlighted a problematic part of your code
Was This Post Helpful? 0
  • +
  • -

#5 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2814
  • View blog
  • Posts: 11,915
  • Joined: 20-September 08

Re: Name and birth date sort

Posted 21 February 2010 - 01:17 PM

Your code is a litle too unnecessarily elaborate. For a start, you don't need to typecheck - comparing an Employee with anything other than an Employee is an error and should be caught as a RuntimeException


  public int compareTo(Object obj) {
    int result = 0;
    Employee worker = (Employee) obj;

    result = lastName.compareToIgnoreCase(worker.lastName);

    if (result == 0) {
      result = firstName.compareToIgnoreCase(worker.firstName);
    }

    if (result == 0) {
      result = birthYear - worker.birthYear;
    }

    return result;
  }


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1