I have a List with my own Objects, called OrderedProducts. I want to sort this List first by an int sequence
, and then by a String name
.
I know how to sort by sequence first, using the following default 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;
}
});
What I want now is to sort by Name within the ordered Sequence. So, for example, I've got the following OrderedProducts in my List:
- Sequence = 2; Name = "AAA";
- Sequence = 4; Name = "AAA";
- Sequence = 7; Name = "BBB";
- Sequence = 2; Name = "CCC";
- Sequence = 1; Name = "ZZZ";
- Sequence = 4; Name = "ZZZ";
- Sequence = 4; Name = "ABC";
This should be sorted like:
5, 1, 4, 2, 7, 6, 3.
Sequence Name
1 "ZZZ"
2 "AAA"
2 "CCC"
4 "AAA"
4 "ABC"
4 "ZZZ"
7 "BBB"