Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
172 lines (143 loc) · 5.5 KB

File metadata and controls

172 lines (143 loc) · 5.5 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package Sort;
import java.util.*;
public class SortJava {
public static void main(String[] args) {
SortJava sj = new SortJava();
// 원시 타입 정렬
System.out.println("==primitiveTypeSort==");
sj.primitiveTypeSort();
// 컬렉션 정렬
System.out.println("\n==collectionSort==");
sj.collectionSort();
// 커스텀 정렬
System.out.println("\n==customSort==");
sj.customSort();
// 오버라이드 커스텀 정렬
System.out.println("\n==overrideCustomSort==");
sj.overrideCustomSort();
// 두 조건 커스텀 정렬 오버라이드
System.out.println("\n==customSortTwoConditionOverride==");
sj.customSortTwoConditionOverride();
// 두 조건 커스텀 정렬
System.out.println("\n==customSortTwoCondition==");
sj.customSortTwoCondition();
}
// 원시타입 정렬
private void primitiveTypeSort() {
int[] nums = {5, 2, 8, 1, 3};
// 오름차순 정렬
Arrays.sort(nums);
System.out.println(Arrays.toString(nums)); // [1, 2, 3, 5, 8]
// 내림차순 정렬 (박싱 필요)
Integer[] boxed = Arrays.stream(nums).boxed().toArray(Integer[]::new);
Arrays.sort(boxed, (a, b) -> b - a);
System.out.println(Arrays.toString(boxed)); // [8, 5, 3, 2, 1]
}
// 컬랙션 정렬
private void collectionSort() {
List<String> list = new ArrayList<>(Arrays.asList("banana", "apple", "cherry"));
// 오름차순 정렬
Collections.sort(list);
System.out.println(list); // [apple, banana, cherry]
// 내림차순 정렬
Collections.sort(list, Collections.reverseOrder());
System.out.println(list); // [cherry, banana, apple]
// Stream + Comparator 활용 (길이 기준 오름차순)
List<String> sortedByLength = list.stream()
.sorted(Comparator.comparingInt(String::length))
.toList();
System.out.println(sortedByLength);
}
private void customSort() {
List<Person> people = List.of(
new Person("Alice", 30),
new Person("Bob", 25),
new Person("Charlie", 28)
);
// 나이 오름차순
List<Person> byAge = new ArrayList<>(people);
byAge.sort(Comparator.comparingInt(p -> p.age));
System.out.println(byAge); // [Bob(25), Charlie(28), Alice(30)]
// 이름 내림차순
List<Person> byNameDesc = new ArrayList<>(people);
byNameDesc.sort(Comparator.comparing((Person p) -> p.name).reversed());
System.out.println(byNameDesc); // [Charlie(28), Bob(25), Alice(30)]
}
private void overrideCustomSort() {
List<Person> people = List.of(
new Person("Alice", 30),
new Person("Bob", 25),
new Person("Charlie", 28)
);
// 나이 오름차순
List<Person> byAge = new ArrayList<>(people);
byAge.sort(new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
// Integer.compare로 안전하게 비교
return Integer.compare(o1.age, o2.age);
}
});
System.out.println(byAge); // [Bob(25), Charlie(28), Alice(30)]
// 이름 내림차순
List<Person> byNameDesc = new ArrayList<>(people);
byNameDesc.sort(new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
// o2.name.compareTo(o1.name) ⇒ 내림차순
return o2.name.compareTo(o1.name);
}
});
System.out.println(byNameDesc); // [Charlie(28), Bob(25), Alice(30)]
}
private void customSortTwoConditionOverride() {
List<Person> people = new ArrayList<>(Arrays.asList(
new Person("Alice", 30),
new Person("Charlie", 28),
new Person("Bob", 25),
new Person("Charlie", 22)
));
Collections.sort(people, new Comparator<Person>() {
@Override
public int compare(Person o1, Person o2) {
// 1) 이름 내림차순
int nameCmp = o2.getName().compareTo(o1.getName());
if (nameCmp != 0) {
return nameCmp;
}
// 2) 이름이 같으면 나이 오름차순
return Integer.compare(o1.getAge(), o2.getAge());
}
});
System.out.println(people);
}
private void customSortTwoCondition() {
List<Person> people = Arrays.asList(
new Person("Alice", 30),
new Person("Charlie", 28),
new Person("Bob", 25),
new Person("Charlie", 22)
);
// 이름 내림차순, 나이 오름차순
people.sort(
Comparator
.comparing(Person::getName, Comparator.reverseOrder())
.thenComparingInt(Person::getAge)
);
System.out.println(people);
}
public static class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public int getAge() { return age; }
@Override
public String toString() {
return name + "(" + age + ")";
}
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.