1

好吧,我的 java 项目快完成了,但还有一个部分要处理,排序。我必须根据员工的 ID 号使用升序排序。我不确定要使用哪种类型,以及在哪里实现它。为它创建一个新类?让我向您展示我的测试代码。我有教职员工/兼职//员工/教育课程都在工作。这些类的代码都显示在 TestEmployee 中。因此,对于如何对我的 ID 号进行排序的任何帮助/提示,​​我们将不胜感激。

import java.util.Calendar;
import java.util.Scanner;

public class TestEmployee implements EmployeeInfo{


        public static void main(String[] args) throws CloneNotSupportedException {

        /**
         * Part A: output all employee from Staff, Faculty, and partime.
         */

        double sum=0,total=0;

                Employee[]emp = new Employee[9];
                Calendar staffBirthDate = Calendar.getInstance();
                Calendar facultyBirthDate = Calendar.getInstance();
                Calendar partimeBirthDate = Calendar.getInstance();

                staffBirthDate.set(1959, 2, 23);// Scott Chan
                emp[0] = new Staff("Chan, Scott", 123, 'M', staffBirthDate, 35.00);

                 staffBirthDate = Calendar.getInstance();

                staffBirthDate.set(1964, 7, 12);// Brian Salinas
                emp[1] = new Staff("Salinas, Brian", 456, 'F', staffBirthDate, 30.00);

                 staffBirthDate = Calendar.getInstance();

                staffBirthDate.set(1970, 6, 2);// Allen Weir
                emp[2] = new Staff("Weir, Allen", 789, 'M', staffBirthDate, 22.00);

                facultyBirthDate = Calendar.getInstance();

                facultyBirthDate.set(1962, 4, 27);
                emp[3] = new Faculty("Im, Lee", 243, 'F', facultyBirthDate, "Full", "PH.D", "Engineering", "3");

                facultyBirthDate = Calendar.getInstance();

                facultyBirthDate.set(1975, 3, 14);// 
                emp[4] = new Faculty("Bui, Thung", 791, 'F', facultyBirthDate, "Associate", "PH.D", "English", "1");

                facultyBirthDate = Calendar.getInstance();


                facultyBirthDate.set(1980, 5, 22);//
                emp[5] = new Faculty("Monreno, Maria", 623, 'F', facultyBirthDate, "Assistant", "MS", "Physical Education", "0");

                 partimeBirthDate = Calendar.getInstance();

                 partimeBirthDate.set(1977, 8, 10);
                emp[6] = new Partime("Lee, Chesong", 455, 'F', partimeBirthDate, 20, 35.00);

                 partimeBirthDate = Calendar.getInstance();


                        partimeBirthDate.set(1987, 9, 15);
                emp[7] = new Partime("Garcia, Frank", 678, 'M', partimeBirthDate, 25, 30.00);

                 partimeBirthDate = Calendar.getInstance();

                        partimeBirthDate.set(1980, 8, 22);//
                emp[8] = new Partime("Alquilo, Roscoe", 945, 'M', partimeBirthDate, 30, 20.00);

                for(int i = 0; i<emp.length;i++)
        {
            if(emp[i] instanceof Staff)
            {
                System.out.println("\n"+emp[i]);
            }//end of if statement
            if(emp[i] instanceof Faculty)
            {
                System.out.println("\n"+emp[i]);
            }//end of if statement

        }// end of for loop

                for(int i = 0; i<emp.length; i++)
                { 
                    sum = ((Employee) emp[i]).monthlyEarning()+sum;
                }
                System.out.println("\nTotal monthly salary for all Employees");

                System.out.println("$"+sum);
                //c
                System.out.println("\nTotal monthly salary for all faculuty");
                for(int i = 0; i<emp.length;i++)
                {
                    if(emp[i] instanceof Faculty)
                    {
                        total = ((Employee) emp[i]).monthlyEarning()+total;
                    }
                }
                System.out.println("$"+total);
              // Duplicate a faculty object. test the duplication
                Faculty f1 = (Faculty)emp[4];
                Faculty f2 = (Faculty)f1.clone();
                Education dupl = new Education("PH.D",
                        "Doctor", "4");
                f2.setEducation(dupl);
                System.out.println("\nD Duplicate a Faculty Object"
                        +"\n"+f2.toString());

                // Verify two staff objects are the same

                System.out.println("\nE.Verify two staff objects ");
                Staff s1 = (Staff)emp[6];
                Staff s2 = (Staff)s1.clone();
                         staffBirthDate = Calendar.getInstance();

                Staff s3 = new Staff("Danger, Norman", 456, 'M', staffBirthDate, 25.00);
                if(s1.getBirthdate()==s2.getBirthdate())
                {
                    System.out.print("\nThe two staff objects " +
                            " birthdays"+ " are the same "
                            +"therefore "+true+"\n");
                }
                if(s3.getBirthdate()==s1.getBirthdate())
                {
                    System.out.print(true);
                }

                // Sort employees by ascending employee ID
                System.out.println("\nSort employees by ID");


                {
                    System.out.println("\n"+emp[i]);
                }
        }
}
4

5 回答 5

2

您通常不会实现排序。您将使用Arrays.sort(在您的情况下)或Collections.sort. 您所要做的就是提供一个Comparator定义排序的方法。

http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html http://docs.oracle.com/javase/6/docs/api/java/util/Arrays.html http://docs.oracle.com/javase/6/docs/api/java/util/Comparator.html

于 2013-09-25T01:09:18.170 回答
0

使用 Java Comparator,或将 Class 设置为 Comparable。然后使用 Arrays.sort()

请参阅此处的示例:http ://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/

于 2013-09-25T01:20:00.757 回答
0
class MyComparator implements Comparator<Employee>{
        public int compare(Employee e1, Employee e2) {
             return Integer.valueOf(e1.id).compareTo(e2.id);
        }
}

MyComparator myComparator = new MyComparator();
Collections.sort(array, myComparator);
于 2013-09-25T01:21:10.370 回答
0

最好利用像 TreeSet 这样的 Java 排序集合,例如:

        Collection<Employee> employList = new TreeSet<Employee>(
                new Comparator<Employee>() {
                    @Override
                    public int compare(Employee a, Employee b) {
                        int getBirthDate = a.getBirthDate()
                                .compareTo(b.getBirthDate());
                        if (getBirthDate != 0)
                            return getBirthDate;
                        int getA = a.getA().compareTo(b.getA());
                        if (getA != 0)
                            return getA;
                        return Long.valueOf(a.getB()).compareTo(Long.valueOf(b.getB()));
                    }
                });
        employList.add(new Employee(...);

先启动采集。然后开始添加 Employee 对象。它们将首先按 BirthDate 自动排序,然后是 Employee 对象的 A 和 B 字段。您可以根据需要添加任意数量或删除 A 并成为一部分,然后返回 a.getBirthdate().compareTo(b.getBirthdate()); 如果你只需要生日。

于 2017-01-06T20:11:45.353 回答
0

我想知道为什么没有人提到 Comparator.natural().reverse()。

于 2017-01-06T20:58:32.283 回答