Java 排序ArrayList以降低姓氏或数字

Java 排序ArrayList以降低姓氏或数字,java,sorting,arraylist,Java,Sorting,Arraylist,我有ArrayList来保存员工的记录。Employee类从Person类扩展而来。我需要对arrayList按姓氏降序排序,然后对每个数据中的薪水进行排序。我已经使用了comparator和collection来进行排序,但它不起作用。这是因为输出部分中没有已排序的数据。请参阅下面我的输出。你能帮我找出它为什么不起作用吗?谢谢 输出: //Show employee's record Full Name: Tom Jones Social Security Number: 123-00-12

我有ArrayList来保存员工的记录。Employee类从Person类扩展而来。我需要对arrayList按姓氏降序排序,然后对每个数据中的薪水进行排序。我已经使用了comparator和collection来进行排序,但它不起作用。这是因为输出部分中没有已排序的数据。请参阅下面我的输出。你能帮我找出它为什么不起作用吗?谢谢

输出:

//Show employee's record

Full Name: Tom Jones
Social Security Number: 123-00-129
Date of Birth: 02-02-1992
Email Address: tj@xyz.com
Phone Number: 408-889-999

Employee ID: 1234A
Employee's salary: 50250.0
When an employee is hired: 01-12-2014
Department Section: Math
Regular hourly rate for a week: 0.0


Full Name: Sarah Carly
Social Security Number: 123-98-0012
Date of Birth: 12-12-1995
Email Address: scarly@gmail.com
Phone Number: 215-551-0001

Employee ID: 9876B
Employee's salary: 0.0
When an employee is hired: 09-06-1989
Department Section: History
Regular hourly rate for a week: 0.0


Full Name: Billy Goodwin
Social Security Number: 001-02-003
Date of Birth: 07-12-1965
Email Address: bgreat@kl.org
Phone Number: 555-555-1133

Employee ID: 0012C
Employee's salary: 100200.0
When an employee is hired: 10-02-1967
Department Section: Administration
Regular hourly rate for a week: 0.0

//This one is supposed to show the sorted data but it is blank.

Sort Order
BUILD SUCCESSFUL (total time: 0 seconds)

*
这是主要内容:

 public static void main(String[] args) {

    ArrayList<Person> people = new ArrayList<Person>();


    Person p2 = new Employee("Tom", "Jones", "02-02-1992", "tj@xyz.com", "408-889-999", "123-00-129", "1234A", 50250, "01-12-2014", 10.2, 42.0, "Math");
    people.add(p2);

    Person p3 = new Employee("Sarah", "Carly", "12-12-1995", "scarly@gmail.com", "215-551-0001", "123-98-0012", "9876B", 0.0, "09-06-1989", 6.7, 56, "History");
    people.add(p3);

    Person p4 = new Employee("Billy", "Goodwin", "07-12-1965", "bgreat@kl.org", "555-555-1133", "001-02-003", "0012C", 100200, "10-02-1967", 0.0, 0, "Administration");
    people.add(p4);

    for(Person list:people) {System.out.println(list);}



    System.out.println("Sort Order");
    Collections.sort(people, byLastName());    


}

private static Comparator<Person> byLastName()
 {
return new Comparator<Person>()
{
    @Override
    public int compare(Person x, Person y)
    {
        return p2.getLastName().compareTo(p3.getLastName());
    }
};        
这是员工级别:

public class Employee extends Person {

    private String empID;
    private double salary;
    private String dateHired;

    private double rate;            
    private double hours;
    private String department;

    public Employee() {

    empID = "";
    salary = 0.0;
    dateHired = "";

    rate = 0.0;
    hours = 0.0;
    department = "";


    }

    public Employee(String firstName, String lastName, String birthDate, String emailAddress, String phoneNum, String socialSecurityNum) {

super(firstName, lastName, birthDate, emailAddress, phoneNum, socialSecurityNum);
    };

    public Employee(String firstName, String lastName, String birthDate, String emailAddress, String phoneNum, String socialSecurityNum, 
    String empID, double salary, String dateHired, double rate, double hour, String department) {

    this.firstName = firstName;
    this.lastName = lastName;
    this.birthDate = birthDate;

    this.emailAddress = emailAddress;
    this.phoneNum = phoneNum;
    this.socialSecurityNum = socialSecurityNum;


    this.empID = empID;
    this.salary = salary;
    this.dateHired = dateHired;

    this.rate = rate;
    this.hours = hours;
    this.department = department;

}


    public String getEmpID() {
            return empID;
}

    public double getSalary() {
            return salary;
}

public String getDateHired() {
    return dateHired;
}

    public double getRate() {
            return rate;
    }

    public double getHours() {
            return hours;
    }

    public String getDepartment() {
            return department;
    }

public void setEmpID(String empID) {
    this.empID = empID;
}

public void setSalary(double salary) {
    this.salary = salary;
}

public void setDateHired(String dateHired) {
    this.dateHired = dateHired;
}

    public void setRate(double rate) {
            this.rate = rate;
    }

    public void setHours(double hours) {
            this.hours = hours;
    }

    public void setDepartment(String department) {
            this.department = department;
    }

    public double calculateHourlyRate(double rate, double hours) {
        return this.rate * this.hours;
    }

    public String EmployeeInfo() {

        return "Employee ID: " + empID + "\nDepartment Section: " + department
                + "\nPay Rate: " + rate + "\nWork Hours: " + hours;
                        }

    public String PersonInfo() {

        String info;

        info = ("\n" +"Full Name: " + firstName + " " + lastName + "\nSocial Security Number: "
                    + socialSecurityNum + "\nDate of Birth: " + birthDate
                    + "\nEmail Address: " + emailAddress + "\nPhone Number: " + phoneNum 
                    + "\n");

            return info; }

    public boolean equals(Employee otherEmployee) {

            return (empID == otherEmployee.empID
            && salary == otherEmployee.salary
            && dateHired == otherEmployee.dateHired
           && rate == otherEmployee.rate
          && hours == otherEmployee.hours
         && department == otherEmployee.department);
    }

    public Employee getCopy() {

            Employee temp = new Employee();

            temp.empID = empID;
            temp.salary = salary;
            temp.dateHired = dateHired;

            temp.rate = rate;
            temp.hours = hours;
            temp.department = department;

            return temp;
    }   

    public void makeCopy(Employee otherEmployee) {

            empID = otherEmployee.empID;
            salary = otherEmployee.salary;
            dateHired = otherEmployee.dateHired;

            rate = otherEmployee.rate;
            hours = otherEmployee.hours;
            department = otherEmployee.department;
    }

    @Override
    public String toString() {

        String display;

        display = ("\n" +"Full Name: " + firstName + " " + lastName + "\nSocial Security Number: "
                    + socialSecurityNum + "\nDate of Birth: " + birthDate
                    + "\nEmail Address: " + emailAddress + "\nPhone Number: " + phoneNum 
                    + "\n" + "\n" +"Employee ID: " + empID + "\nEmployee's salary: "
                    + salary + "\nWhen an employee is hired: " + dateHired
                    + "\nDepartment Section: " + department + "\nRegular hourly rate for a week: " 
                    + calculateHourlyRate(rate, hours) + "\n");

            return display;
}

    public void PrintEmployeeInfo() {

        System.out.print(EmployeeInfo());
    }

    public void PrintPersonInfo() {

        System.out.print(PersonInfo());
    }

    public void PrintCalculateHourlyRate() {

        System.out.print(calculateHourlyRate(rate, hours));
    }

在比较器中,您总是比较p2和p3,它可能是这样的:

@Override
    public int compare(Person x, Person y)
    {
        return x.getLastName().compareTo(y.getLastName());
    }
此外,如果要在多个特性上进行比较,则需要更改计算以包括其他特性


您没有得到任何排序输出的原因是您只打印未排序的列表。如果您想输出排序后的列表,您需要在
Collections.sort(people,byLastName())之后再次为(Person list:people){System.out.println(list);}
执行
行<代码>集合。排序
仅对其排序,不会打印

它不起作用了
?这是什么意思?不是,但你应该一直解释你的问题。说它不起作用类似于去看医生,说我头痛,然后医生问你的症状,你说我只知道我的头痛……当我运行程序时,输出部分没有排序数据。它应该在那里,所以我假设它不起作用。@user3429089排序之前您正在打印列表。是的,我想在排序之前显示列表。我不知道如何打印已排序的列表。哦,我的错误!我刚把它删掉了,但还是不起作用。我的意思是它没有对姓氏排序。输出部分中没有已排序的数据。
@Override
    public int compare(Person x, Person y)
    {
        return x.getLastName().compareTo(y.getLastName());
    }