首页 文章

对 ArrayList 进行排序以降落姓氏或数字[1]

提问于
浏览
-2

我有 ArrayList 来保存员工的记录。员工班级从人员班级扩展。我需要按姓氏的降序对 arrayList 进行排序,然后在每个数据中按薪水排序。我已经使用比较器和集合来进行排序,但是没有用。这是因为在输出部分中没有排序的数据。请参阅下面的输出。您能帮我找出为什么它不起作用吗?谢谢!

输出:

//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());
    }
};

}

这是 Person 类:

public class Person {

    String firstName;
    String lastName;
    String emailAddress;

    String birthDate;
    String phoneNum;
    String socialSecurityNum;

    public Person() {

    firstName = "";
    lastName = "";
    birthDate = "";

    emailAddress = "";
    phoneNum = "";
    socialSecurityNum = "";

    }

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

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

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

    }

    public String getFirstName() {
            return firstName;
}

    public String getLastName() {
            return lastName;
}

public String getBirthDate() {
    return birthDate;
}

    public String getEmailAddress() {
            return emailAddress;
    }

    public String getPhoneNum() {
            return phoneNum;
    }

    public String getSocialSecurityNum() {
            return socialSecurityNum;
    }

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

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

public void setBirthDate(String birthDate) {
    this.birthDate = birthDate;
}

    public void setEmailAddress(String emailAddress) {
            this.emailAddress = emailAddress;
    }

    public void setPhoneNum(String phoneNum) {
            this.phoneNum = phoneNum;
    }

    public void setSocialSecurityNum(String socialSecurityNum) {
            this.socialSecurityNum = socialSecurityNum;
    }

    @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");

            return display;
}

int compareTo(Person get) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

 }

这是 Employee 类:

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));
    }

1 回答

  • 2

    在比较器中,您始终在比较 p2 和 p3,可能应该是这样的:

    @Override
        public int compare(Person x, Person y)
        {
            return x.getLastName().compareTo(y.getLastName());
        }
    

    此外,如果要比较多个属性,则需要更改评估以同时包含其他属性。

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

相关问题