MrT*_*eos 0 java arrays sorting
假设我有一组Employee对象.
单个员工对象具有以下参数:名称,年龄和部门.
我需要按年龄对Employee对象进行排序.
除了一部分,我知道如何做到这一点.
当我使用compareTo方法时,我需要指定如何对整数进行排序.
如何使它们成为整数或列表数组?
编辑
这是我的代码,以进一步阐明我需要做什么.
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
Run Code Online (Sandbox Code Playgroud)
如您所见,每个Employee对象都有3个参数.
我需要按它们的年龄整数对它们进行排序.
你应该看看sort这个Arrays类的方法
public static <T> void sort(T[] a, Comparator<? super T> c)
您需要创建一个Comparator告诉它如何订购您的员工
http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html
同样,如果你有一种List或其他类型的Collection你可以使用的sort方法Collections