Cześć.
Mam własny typ który składa się z dwóch intów. Chciałbym go posortować najpierw wg pierwszego inta, a następnie sortować "wewnętrznie" wg drugiego inta. Wyglądało by to tak. Weźmy pray: (2,4),(1,3),(1,2),(2,2),(3,1),(3,0)
Najpierw sortujemy wg pierwszego i dostajemy coś takiego: (1,3),(1,2),(2,4),(2,2),(3,1),(3,0)
Następnie wg drugiego ```
(1,2),(1,3),(2,2),(2,4),(3,0),(3,1)
```java
public class Test {
private int a;
private int b;
public Test(int a, int b) {
this.a = a;
this.b = b;
}
public int getA() {
return a;
}
public int getB() {
return b;
}
@Override
public String toString() {
return a + " " + b;
}
public static void main(String[] args) {
List<Test> testList = new LinkedList<>();
ThreadLocalRandom current = ThreadLocalRandom.current();
for (int i = 0; i < 10; i++) {
testList.add(new Test(current.nextInt(0, 10), current.nextInt(0, 10)));
}
testList.forEach(System.out::println);
testList.sort(Comparator.comparingInt(Test::getA));
System.out.println("---");
testList.forEach(System.out::println);
}
Comparator.comparing(Test::getA).thenComparing(Test::getB)
sam oceń co jest czytelniejsze.else
w twoim kodzie jest zbędny.