I am trying to sort a List<List<Integer>>
lexicographical manner. But I'm unable to achieve the goal and don't get where is the issue.
List<List<Integer>> result = new ArrayList<List<Integer>>();
result.add(Arrays.asList(1, 3, 76, 99));
result.add(Arrays.asList(1, 2, 84, 92));
result.add(Arrays.asList(1, 1, 76, 99));
java.util.Collections.sort(result, (item1, item2) -> {
if (item1.get(0) > item2.get(0) || item1.get(1) > item2.get(1) || item1.get(2) > item2.get(2)
|| item1.get(3) > item2.get(3)) {
return 1;
} else {
return -1;
}
});
Expected output: [[1, 1, 76, 99], [1, 2, 84, 92], [1, 3, 76, 99]]
But I'm getting [[1, 3, 76, 99], [1, 2, 84, 92], [1, 1, 76, 99]]
I want that index wise smallest number will come first. In the example, all three lists have 1 at the first position, so no change. At the second position, 3rd list has 1 which is the minimum among the three so 3rd list will be moved to the first. Then considering the 2nd item of 2nd and 3rd list 2 is lower so second list will remain at the 2nd position.