【发布时间】:2014-07-22 07:04:23
【问题描述】:
我有一个包含我自己的对象的列表,称为 OrderedProducts。我想先按int sequence 排序此列表,然后按String name。
我知道如何先按顺序排序,使用以下默认的 Collections.sort:
Collections.sort(orderedProductsList, new Comparator<OrderedProduct>(){
@Override
public int compare(OrderedProduct op1, OrderedProduct op2){
if(op1.getSequence() < op2.getSequence())
return -1;
else if(op1.getSequence() > op2.getSequence())
return 1;
else // op1.getSequence() == op2.getSequence()
return 0;
}
});
我现在想要的是在有序序列中按名称排序。因此,例如,我的 List 中有以下 OrderedProducts:
- 序列 = 2;名称 = “AAA”;
- 序列 = 4;名称 = “AAA”;
- 序列 = 7;名称 = "BBB";
- 序列 = 2;名称 = "CCC";
- 序列 = 1;名称 = "ZZZ";
- 序列 = 4;名称 = "ZZZ";
- 序列 = 4;名称 = "ABC";
这应该是这样排序的:
5, 1, 4, 2, 7, 6, 3.
Sequence Name
1 "ZZZ"
2 "AAA"
2 "CCC"
4 "AAA"
4 "ABC"
4 "ZZZ"
7 "BBB"
【问题讨论】:
标签: java android list sorting arraylist