0

Let's say I have an array of Employee objects.

A single employee object has parameters such as : name, age and department.

I need to sort the Employee objects by their age.

I know how to do this except for one part.

When I use the compareTo method, I need to specify how to sort the integers.

How would I do this with making them an array of integers or list?

EDIT

Here is my code to further clarify what I need to do.

public class Company {
    public static void main(String [] args){
        Employee[] e = new Employee[13];
        PrimeAgeChecker p = new PrimeAgeChecker();
        Department d = new Department();
        e[0] = new Employee("Counting Guru",55,"Accounting");
        e[1] = new Employee("Counting Pro",45,"Accounting");
        e[2] = new Employee("Counting Savvy",40,"Accounting");
        e[3] = new Employee("Counting Novice",25,"Accounting");
        e[4] = new Employee("Sales Guru",50,"Marketing");
        e[5] = new Employee("Sales Pro",48,"Marketing");
        e[6] = new Employee("Sales Savvy",38,"Marketing");
        e[7] = new Employee("Hiring Guru",58,"Human Resrouces");
        e[8] = new Employee("Hiring Pro",47,"Human Resrouces");
        e[9] = new Employee("Hacking Pro",47,"Information Systems");
        e[10] = new Employee("Hacking Guru",51,"Information Systems");
        e[11] = new Employee("Hacking Savvy",38,"Information Systems");
        e[12] = new Employee("Hacking Novice",23,"Information Systems");


        for(int i = 0;i<e.length;i++){
            System.out.println(e[i] + " " + p.isPrime(e[i]));
        }//end 
    }//end main
}//end company

As you can see, each Employee object takes 3 parameters.

I need to sort them by the integer which is their age.

6
  • 3
    No array, No list? . Then how are you storing them?? Commented Mar 18, 2014 at 3:54
  • 1
    I think he just means without putting all the ints in an array as he already has the employees in an array Commented Mar 18, 2014 at 3:55
  • It's not possible without some form of data structure to hold the sorted order objects. Commented Mar 18, 2014 at 3:56
  • I edited the post to further clarify what I need to do. Commented Mar 18, 2014 at 3:59
  • "When I use the compareTo method, I need to specify how to sort the integers." - I am afraid I don't quite understand this statement Commented Mar 18, 2014 at 4:02

1 Answer 1

8

You should have a look at the sort method of the Arrays class

public static <T> void sort(T[] a, Comparator<? super T> c)

You will need to create a Comparator to tell it how to order your employees

http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html

Similarly, if you had a List or other type of Collection you could use the sort method of Collections

Sign up to request clarification or add additional context in comments.

3 Comments

+1 An alternative to creating a Comparator is to have Employee implement Comparable, comparing ages.
@JasonC I'd say that's a bad habit, though; only inherent natural orders should be implemented directly on the class.
@chrylis - That is true. On that happy note, I have deleted my answer, in which I did exactly that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.