Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Now I use linq4j.I can only sort one property.How to sort the list which object has more than one property?If linq4j do not have this function,what about other method ?My code is here:
Java
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();

  }
 }

The code show to sort the list by name,but if I want to sort the list by name,and then by deptno,how to do that?

What I have tried:

I have try to sort like this,but it do not work.
Java
List<Employee> filter=Linq4j.asEnumerable(Arrays.asList(emps))
               .orderBy(new Function1<Employee,String>()
           {
           public String apply(Employee arg0)
           {
               return arg0.name;
           }

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

       }).toList();
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900