java 如何按姓名对员工进行排序,如果姓名重复,则按姓氏排序,以及如何按工资排序[已关闭]

vsdwdz23  于 2023-03-16  发布在  Java
关注(0)|答案(2)|浏览(230)

已关闭。此问题需要details or clarity。当前不接受答案。
**想要改进此问题?**添加详细信息并通过editing this post阐明问题。

21小时前关门了。
此帖子在14小时前编辑并提交审查。
Improve this question
假设我们有以下Employees列表:

List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee(40000d, "Abhijeet","Rakshit"));
employeeList.add(new Employee(30000d, "Sunil","Sawant"));
employeeList.add(new Employee(20000d, "Abhijeet", "Ranade"));
employeeList.add(new Employee(40000d, "Srikanth", "Ranka"));

我想按first name排序,如果相等,按last name排序,然后按salary排序。

pkwftd7m

pkwftd7m1#

使用一个比较器和list.sort方法。只有在前面的比较返回equal时才调用后续的thenComparing调用。我更喜欢姓氏而不是薪水作为决胜局。如果需要,它们可以交换。

record Employee(double getSalary, String getFirstName, String getLastName) {
}

List<Employee> employeeList = new ArrayList<>();
employeeList.add(new Employee(40000, "Abhijeet","Rakshit"));
employeeList.add(new Employee(30000, "Sunil","Sawant"));
employeeList.add(new Employee(20000, "Abhijeet", "Ranade"));
employeeList.add(new Employee(40000, "Srikanth", "Ranka"));
        
employeeList.sort(Comparator.comparing(Employee::getFirstName)
         .thenComparing(Employee::getLastName)
         .thenComparing(Employee::getSalary));
      

employeeList.forEach(System.out::println);

印刷品

Employee[getSalary=40000.0, getFirstName=Abhijeet, getLastName=Rakshit]
Employee[getSalary=20000.0, getFirstName=Abhijeet, getLastName=Ranade]
Employee[getSalary=40000.0, getFirstName=Srikanth, getLastName=Ranka]
Employee[getSalary=30000.0, getFirstName=Sunil, getLastName=Sawant]
w1jd8yoj

w1jd8yoj2#

**在下面的类中有两种方法。一种使用stream.sorted,另一种是list.sort方法。

1.当您不想修改原始集合,同时又想使用像collect或filter这样的流操作时,第一种方法会很有用。
1.第二种方法速度更快,并且始终会修改原始列表**

import java.util.ArrayList;
   import java.util.Comparator;
   import java.util.List;
   import java.util.stream.Collectors;
   
   public class SortEmployeesJava8 {
   
       public static void main(String args[]) {
   
           Comparator<Employee> employeeNameComparator = Comparator.comparing(Employee::getName);
   
           Comparator<Employee> employeeLastNameComparator = Comparator.comparing(Employee::getLastName);
   
           Comparator<Employee> employeeSalaryComparator = Comparator.comparing(Employee::getSalary);
   
           SortEmployeesJava8 test = new SortEmployeesJava8();
   
           List<Employee> employeeList = test.getEmployeeList();
   
           System.out.println("Sort by Name Output \n");
   
   
           //List<Employee> sortedByNameEmployeeList =
                   employeeList
                   .sort(employeeNameComparator
                           .thenComparing(employeeLastNameComparator));
                   //.collect(Collectors.toList());
   
           employeeList.stream()
                   .forEach(e -> System.out.println("name : " + e.getName() + " " + e.getLastName()));
   
           System.out.println("\n Sort by Salary Output \n");
   
           List<Employee> sortedBySalaryEmployeeList = employeeList.stream()
                   .sorted(employeeSalaryComparator)
                   .collect(Collectors.toList());
   
           sortedBySalaryEmployeeList.stream()
                   .forEach(e -> System.out.println("name : " + e.getName() + " " + e.getLastName()+" Salary :"+e.getSalary()));
   
           employeeList.sort(Comparator.comparing(Employee::getName)
                   .thenComparing(Employee::getLastName)
                   .thenComparing(Employee::getSalary));
   
           System.out.println("\n Sort by using sort function of list ->Output \n");
   
           employeeList.stream().forEach(e -> System.out.println("name : " + e.getName() + " " + e.getLastName()+" Salary :"+e.getSalary()));
   
   
       }
   
       class Employee {
           Double salary;
           String name;
   
           String lastName;
   
           Employee(Double salary, String name, String lastName) {
               this.salary = salary;
               this.name = name;
               this.lastName = lastName;
           }
   
           public Double getSalary() {
               return this.salary;
           }
   
           public void setSalary(Double salary) {
               this.salary = salary;
           }
   
           public String getName() {
               return this.name;
           }
   
           public void setName(String name) {
               this.name = name;
           }
   
           public String getLastName() {
               return lastName;
           }
   
           public void setLastName(String lastName) {
               this.lastName = lastName;
           }
   
       }
   
       public List<Employee> getEmployeeList() {
           List<Employee> employeeList = new ArrayList<>();
           employeeList.add(new Employee(40000d, "Abhijeet", "Rakshit"));
           employeeList.add(new Employee(30000d, "Sunil", "Sawant"));
           employeeList.add(new Employee(20000d, "Abhijeet", "Ranade"));
           employeeList.add(new Employee(40000d, "Srikanth", "Ranka"));
           return employeeList;
       }
   
   
   }

   
       

Sort by Name Output

name : Abhijeet Rakshit
name : Abhijeet Ranade
name : Srikanth Ranka
name : Sunil Sawant

Sort by Salary Output 

name : Abhijeet Ranade Salary :20000.0
name : Sunil Sawant Salary :30000.0
name : Abhijeet Rakshit Salary :40000.0
name : Srikanth Ranka Salary :40000.0

Sort by using sort function of list ->Output 

name : Abhijeet Rakshit Salary :40000.0
name : Abhijeet Ranade Salary :20000.0
name : Srikanth Ranka Salary :40000.0
name : Sunil Sawant Salary :30000.0
```

相关问题