【发布时间】:2016-11-12 10:34:21
【问题描述】:
我有两门课:
public class Invoice {
private int InvoiceId;
private Customer customer;
private Date invoiceDate;
}
还有
public class Customer {
private int customerId;
private String customerType;
}
customerType 字段可以有三个值:F,H,B
我有一个 Invoice 对象的 arrayList,我想使用 customerType 属性按特定顺序对其进行排序:所有带有 customerType“F”的发票,然后是“H”然后是“B”的发票。
我尝试实现 Comparator 接口,但问题是他的方法 compare 接受 Customer 对象的两个参数,并且以升序或降序方式进行排序。 这是代码:
Collections sort(invoicesList, new Comparator<Invoice >() {
public int compare(Invoice s1,Invoice s2) {
return s1.getCustomer().getCustomerTpe().equalsIgnoreCase("F").compareTo(s2.getCustomer().getCustomerTpe().equalsIgnoreCase("H"))
}
}
);
有没有办法以具有三个字段值的特定顺序对我的收藏进行排序? 谢谢
【问题讨论】:
标签: sorting collections comparator