我有ArrayList来保存员工的记录。 Employee类是从Person类扩展而来的。我需要按照姓氏的降序对arrayList进行排序,然后在每个数据中对salary进行排序。我已经使用比较器和集合来进行排序,但它没有用。这是因为输出部分没有排序数据。请参阅下面的输出。你能帮我找到它不起作用的原因吗?谢谢!
输出:
//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.
}
}
这是员工类:
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));
}
答案 0 :(得分:2)
在比较器中你总是比较p2和p3,它应该是这样的:
@Override
public int compare(Person x, Person y)
{
return x.getLastName().compareTo(y.getLastName());
}
此外,如果要比较多个属性,则需要更改评估以包含其他属性。
您没有获得任何排序输出的原因是您只打印未排序的列表。如果要输出已排序的列表,则需要在for(Person list:people) {System.out.println(list);}
行后再次Collections.sort(people, byLastName());
。 Collections.sort
只对其进行排序并且不会打印出来。