public class Customer {
String loanNumber;
Date acctStartDate;
String firstName;
String lastName;
}
我有List对象,希望按acctStartDate
然后loanNumber
按顺序排序。
如果两个帐户在同一天开始,即两个对象具有相同的acctStartDate,则应使用loanNumber进一步排序
So the priority is
First : acctStartDate
Second : loanNumber
这里loanNumber是String。有人请帮助我:(
答案 0 :(得分:0)
您可以使用Apache Commons库中的CompareToBuilder
类,并通过调用append()来添加多个排序约束。请尝试以下代码:
import org.apache.commons.lang3.builder.CompareToBuilder;
....
Collections.sort(list, new Comparator<Customer>() {
@Override
public int compare(Customer obj1, Customer obj2) {
return new CompareToBuilder().append(obj1.acctStartDate, obj2.acctStartDate)
.append(obj1.loanNumber, obj2.loanNumber).toComparison();
}
});