简体   繁体   English

如何根据给定的条件使用 Comparator.comparing() 对类进行排序

[英]How to sort class using Comparator.comparing() depending on given criteria

I got following class which is not compilable code.我得到了以下不可编译代码的类。 I want to send criteria as a parameter to sort the employees array.我想发送标准作为参数来对员工数组进行排序。 How can I do it by using Comparator.comparing() in Java 8 functional style?如何通过在 Java 8 函数风格中使用 Comparator.comparing() 来做到这一点? For example, I want to sort it by name or by age, etc.例如,我想按姓名或年龄等对其进行排序。

public class Employee {
        String name;
        int age;
        double salary;
        long mobile;    
}


public class EmpolyeeSorter {
    Employee[] employees = new Employee[]{
            new Employee("John", 25, 3000.0, 9922001),
            new Employee("Ace", 22, 2000.0, 5924001),
            new Employee("Keith", 35, 4000.0, 3924401)};


    public static void sortEmpoyeeByCriteria(Function<? super T, ? extends U> byCriteria) {
        Comparator<Employee> employeeComparator
                = Comparator.comparing(byCriteria);

    }
}

You are missing a bit the definition of the of your method:您缺少一些方法的定义:

public static <T, U extends Comparable<? super U>> void sortEmpoyeeByCriteria(Function<? super T, ? extends U> byCriteria) {
        Comparator<T> c = Comparator.comparing(byCriteria);
}

You should also pass the List to that method that you want to sort in this case.在这种情况下,您还应该将List传递给要排序的方法。

But then why not use Collections.sort in this case and pass the List and the Comparator in the form (for example):但是为什么不在这种情况下使用Collections.sort并以表单(例如)传递ListComparator

Collections.sort(yourList, Comparator.comparing(Employee::getXXX))

EDIT You could declare it like this:编辑你可以这样声明:

public static <T, U extends Comparable<? super U>> void sortEmpoyeeByCriteria(
            List<T> list,
            Function<? super T, ? extends U> byCriteria) {
        Comparator<? super T> c = Comparator.comparing(byCriteria);
        list.sort(c);
}

You can use a reference method您可以使用参考方法

public static void main(String[] args) {
    Employee[] employees = new Employee[] { new Employee("John", 25, 3000.0, 9922001),
            new Employee("Ace", 22, 2000.0, 5924001), new Employee("Keith", 35, 4000.0, 3924401) };


    Comparator<Employee> comparingAge = Comparator.comparing(Employee::getAge);
    Comparator<Employee> comparingName = Comparator.comparing(Employee::getName);
    Comparator<Employee> comparingSalary = Comparator.comparing(Employee::getSalary);

    Arrays.sort(employees, comparingAge);
    System.out.println(Arrays.toString(employees));

    Arrays.sort(employees, comparingName);
    System.out.println(Arrays.toString(employees));

    Arrays.sort(employees, comparingSalary);
    System.out.println(Arrays.toString(employees));
}

you can do too:你也可以这样做:

    sortMe(employees, comparingAge);
    sortMe(employees, comparingName);
    sortMe(employees, comparingSalary);

where sortme is : sortme 在哪里:

private static void sortMe(Employee[] employees, Comparator<Employee> comparingCriteria) {
    Arrays.sort(employees, comparingCriteria);
}

Tried lambda approach in Comparator.comparing()在 Comparator.comparing() 中尝试了 lambda 方法

class Employee {

  String name;
  int age;
  double salary;
  long mobile;

public Employee(String name, int age, double salary, long mobile) {
    this.name = name;
    this.age = age;
    this.salary = salary;
    this.mobile = mobile;
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public int getAge() {
    return age;
}
public void setAge(int age) {
    this.age = age;
}
public double getSalary() {
    return salary;
}
public void setSalary(double salary) {
    this.salary = salary;
}
public long getMobile() {
    return mobile;
}
public void setMobile(long mobile) {
    this.mobile = mobile;
}

@Override
public String toString() {
    return "Employee [name=" + name + ", age=" + age + ", salary=" + salary + ", mobile=" + mobile + "]";
 }
}


public class MainClass
 {
    public static List<Employee> getEmployee()
    {
        List<Employee> ob = new ArrayList<>();
        ob.add(new Employee("John", 25, 3000.0, 9922001));
        ob.add(new Employee("Ace", 22, 2000.0, 5924001));
        ob.add(new Employee("Keith", 35, 4000.0, 3924401));
        return ob;
    }

    public static void main(String a[]) {
        List<Employee> ob = getEmployee();

        System.out.println("Sort By Name");
        ob.sort(Comparator.comparing(e -> e.getName()));
        System.out.println(ob);

        System.out.println("Sort By Age");
        ob.sort(Comparator.comparing(e -> e.getAge()));
        System.out.println(ob);         
    }
}

Output:输出:

Sort By Name
[Employee [name=Ace, age=22, salary=2000.0, mobile=5924001], Employee [name=John, age=25, salary=3000.0, mobile=9922001], Employee [name=Keith, age=35, salary=4000.0, mobile=3924401]]
Sort By Age
[Employee [name=Ace, age=22, salary=2000.0, mobile=5924001], Employee [name=John, age=25, salary=3000.0, mobile=9922001], Employee [name=Keith, age=35, salary=4000.0, mobile=3924401]]

Hope this help!!希望这有帮助!!

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

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