-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTupleList2.java
More file actions
31 lines (29 loc) · 1.09 KB
/
TupleList2.java
File metadata and controls
31 lines (29 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
public class TupleList2 {
public static <A extends Comparable<A>, B extends Comparable<B>>
void sortTupleList(List<Tuple2<A, B>> tupleList) {
tupleList.sort(new Comparator<Tuple2<A, B>>() {
public int compare(Tuple2<A, B> tuple1, Tuple2<A, B> tuple2) {
int firstComp = tuple1.getFirst().compareTo(tuple2.getFirst());
if (firstComp != 0) {
return firstComp;
} else {
return tuple1.getSecond().compareTo(tuple2.getSecond());
}
}
});
}
public static void main(String[] args) {
List<Tuple2<Integer, String>> tupleList = new ArrayList<>();
tupleList.add(new Tuple2<>(1, "hi"));
tupleList.add(new Tuple2<>(1, "bye"));
tupleList.add(new Tuple2<>(2, "hi"));
tupleList.add(new Tuple2<>(2, "bye"));
sortTupleList(tupleList);
for (Tuple2<Integer, String> tuple : tupleList) {
System.out.println(tuple);
}
}
}