gpt4 book ai didi

java - 如何按对象的多个属性进行排序 - linq4j

转载 作者:行者123 更新时间:2023-11-30 06:51:22 24 4
gpt4 key购买 nike

我正在使用 linq4j。我只按一个属性排序,并想按多个属性排序。如果 linq4j 不支持,那么我可以使用其他方法。以下是我的代码片段,

import java.util.Arrays;
import java.util.List;
import net.hydromatic.linq4j.Linq4j;
import net.hydromatic.linq4j.function.*;

public class Linq4jExample {
public static class Employee {
public final int empno;
public final String name;
public final int deptno;

public Employee(int empno, String name, int deptno) {
this.empno = empno;
this.name = name;
this.deptno = deptno;
}

public String toString() {
return "Employee(empno: " + empno +",name: " + name + ", deptno:" + deptno + ")";
}
}

public static final Employee[] emps = {
new Employee(100, "Fred", 10),
new Employee(110, "Bill", 30),
new Employee(120, "Bill", 10),
new Employee(120, "Eric", 12),
new Employee(130, "Janet", 13),
};

public static final Function1<Employee, Integer> EMP_DEPTNO_SELECTOR =
new Function1<Employee, Integer>() {
public Integer apply(Employee employee) {
return employee.deptno;
}
};

public static void main(String[] args) {

List<Employee> filter=Linq4j.asEnumerable(Arrays.asList(emps))
.orderBy(new Function1<Employee,String>()
{
public String apply(Employee arg0)
{
return arg0.name;
}

}).toList();

}
}

潜在的用例可以先按部门编号排序,然后按名称排序

最佳答案

在 linq4j 中您可以有多个 order by。检查以下示例

package maven_lab;

import java.util.Arrays;
import java.util.List;

import org.apache.calcite.linq4j.Linq4j;
import org.apache.calcite.linq4j.function.Function1;

public class Linq4jExample {

public static final Employee[] emps = {
new Employee(100, "Fred", 10),
new Employee(110, "Bill", 30),
new Employee(120, "Bill", 10),
new Employee(120, "henry", 10),
new Employee(120, "Adam", 10),
new Employee(120, "Eric", 12),
new Employee(130, "Janet", 13),
};

public static final Function1<Employee, Integer> EMP_DEPTNO_SELECTOR =
new Function1<Employee, Integer>() {
public Integer apply(Employee employee) {
return employee.deptno;
}
};

public static void main(String[] args) {

List<Employee> filter = Linq4j.asEnumerable(Arrays.asList(emps)).orderBy(e -> e.deptno)
.orderBy(e -> e.name).toList();

System.out.println(filter);

}

public static class Employee {
public final int empno;
public final String name;
public final int deptno;

public Employee(int empno, String name, int deptno) {
this.empno = empno;
this.name = name;
this.deptno = deptno;
}

public String toString() {
return "Employee(empno: " + empno + ",name: " + name + ", deptno:" + deptno + ")";
}
}


}

请注意,我首先按部门排序,然后按名称排序。您还应该使用 jdk8 lambda 而不是匿名类。

<小时/>

在 JDK8 之前,您可以按照以下方式执行此操作,

 List<Employee> filter = Linq4j.asEnumerable(Arrays.asList(emps)).orderBy(new Function1<Employee, Integer>() {
public Integer apply(Employee arg0) {
return arg0.deptno;
}

}).orderBy(new Function1<Employee, String>() {
public String apply(Employee arg0) {
return arg0.name;
}

}).toList();

关于java - 如何按对象的多个属性进行排序 - linq4j,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42710374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com