-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortMain2.java
More file actions
38 lines (29 loc) · 1.13 KB
/
Copy pathSortMain2.java
File metadata and controls
38 lines (29 loc) · 1.13 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
32
33
34
35
36
37
package collection.compare;
import java.util.*;
import java.io.*;
public class SortMain2 {
public static void main(String[] args) {
Integer[] array = {3, 2, 1};
System.out.println(Arrays.toString(array));
System.out.println("Comparator 비교");
Arrays.sort(array, new AscComparator());
System.out.println("AscComparator: " + Arrays.toString(array));
Arrays.sort(array, new DescComparator());
System.out.println("DescComparator:" + Arrays.toString(array));
Arrays.sort(array, new AscComparator().reversed());
System.out.println("AscComparator.reversed:" + Arrays.toString(array));
}
static class AscComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
System.out.println("o1=" + o1 + " o2=" + o2);
return (o1 < o2) ? -1 : ((o1 == o2) ? 0 : 1);
}
}
static class DescComparator implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return (o1 < o2) ? -1 : ((o1 == o2) ? 0 : 1) * -1;
}
}
}