-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava187_Sort.java
More file actions
62 lines (47 loc) · 1.34 KB
/
Copy pathJava187_Sort.java
File metadata and controls
62 lines (47 loc) · 1.34 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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package java0912_collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.ListIterator;
// 오름차순
class Ascending implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
System.out.println((o1 + "/ " + o2 + " ") + o1.compareTo(o2));
return o1.compareTo(o2);
}
}
// 내림차순
class Descending implements Comparator<Integer> {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
}
public class Java187_Sort {
public static void main(String[] args) {
Integer[] arr = new Integer[] { 1, 3, 5, 2, 4 };
ArrayList<Integer> aList = new ArrayList<Integer>(Arrays.asList(arr));
// 오름차순
aList.sort(new Ascending());
System.out.println(aList);
// 내림차순
aList.sort(new Descending());
System.out.println(aList);
System.out.println("//////////////////");
Collections.sort(aList);
System.out.println(aList);
Collections.sort(aList, new Descending());
System.out.println(aList);
System.out.println("///////////////////");
ListIterator<Integer> ite = aList.listIterator();
while (ite.hasNext()) {
System.out.println(ite.next());
}
System.out.println("===================");
while (ite.hasPrevious()) {
System.out.println(ite.previous());
}
}
}