简体   繁体   English

有没有办法使用 lambda 表达式比较器对多个条件进行排序

[英]Is there a way to perform sorting on multiple criteria using lambda expression comparator

I am trying to sort an array of students by various criteria.I want to sort by their first name and if that is the same then by their last name.我正在尝试按各种标准对一组学生进行排序。我想按他们的名字排序,如果相同,则按他们的姓氏排序。 If both names are the same then by an ID number.如果两个名称相同,则使用 ID 号。 I am currently using a lambda expression which successfully sorts them by their first name.我目前正在使用 lambda 表达式,它成功地按名字对它们进行排序。 Is there a way to pass the other sorting criteria to the expression?有没有办法将其他排序标准传递给表达式? (There are getter methods similar to getFirstName() for the ID and the last name). (对于 ID 和姓氏,有类似于 getFirstName() 的 getter 方法)。 I have written a generic quicksort algorithm to which I pass my custom comparator interface, IStudentComparator.我编写了一个通用快速排序算法,我将自定义比较器接口 IStudentComparator 传递给该算法。 This is my code for the first criteria:这是我的第一个标准的代码:

QuickSort<Student> myNameSort = new QuickSort<>();
IStudentComparator compare = (Student a, Student b) 
    -> (a.getFirstName().compareTo(b.getFirstName()) < 0);
        
myNameSort.quickSort(myStudents,0,myStudents.length-1,compare); 

What stops you from using the fields in your comparator?是什么阻止您使用比较器中的字段?

IStudentComparator compare (Student a, Student b) -> {
  // compare first name
  // compare last name if first name was equal
  // return comparison of ID number if last name was equal
}

Here is a small class file that depicts how you can use a lambda expression to sort a list of objects by multiple properties.这是一个小的 class 文件,描述了如何使用 lambda 表达式按多个属性对对象列表进行排序。

Java 8 has the method thenComparing with the help of which you can compare by multiple properties. Java 8 有 thenComparing 的方法,可以通过多个属性进行比较。

public class SortByMultiple {

    public static void main(String[] args) {
        ArrayList<Employee> employees = getUnsortedEmployeeList();

        //Compare by first name and then last name id
        Comparator<Employee> compareByName = Comparator
                .comparing(Employee::getId)
                .thenComparing(Employee::getLastName)
                .thenComparing(Employee::getFirstName);

        List<Employee> sortedEmployees = employees.stream()
                .sorted(compareByName)
                .collect(Collectors.toList());

        System.out.println(sortedEmployees);
    }

    private static ArrayList<Employee> getUnsortedEmployeeList() {
        ArrayList<Employee> list = new ArrayList<>();
        list.add(new Employee(1l, "Alex", "Gussin"));
        list.add(new Employee(4l, "Brian", "Sux"));
        list.add(new Employee(5l, "Neon", "Piper"));
        return list;
    }

    public static class Employee {

        Long id;
        String firstName;
        String lastName;

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getFirstName() {
            return firstName;
        }

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

        public String getLastName() {
            return lastName;
        }

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

        public Employee(Long id, String firstName, String lastName) {
            this.id = id;
            this.firstName = firstName;
            this.lastName = lastName;
        }

        @Override
        public String toString() {
            return "Employee{" + "id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + '}';
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM