반응형
Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 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 |
Tags
- ArrayList
- 인텔리제이
- IntelliJ
- Eclipse
- 자바
- html
- 배열
- table
- json
- replace
- CMD
- Array
- Visual Studio Code
- input
- 정규식
- 자바스크립트
- 이탈리아
- date
- Java
- 문자열
- list
- HashMap
- Button
- js
- 이클립스
- string
- javascript
- CSS
- map
- vscode
Archives
- Today
- Total
어제 오늘 내일
[Java 기초] Collections.sort()로 리스트 정렬하기 본문
배열은 Arrays.sort()로 정렬할 수 있지만, **리스트(List)**를 정렬할 때는 어떻게 해야 할까요?
그럴 때 사용하는 것이 바로 **Collections.sort()**입니다.
이 메서드는 리스트(List)를 정렬할 때 사용하는 대표적인 유틸리티 메서드입니다.
1. 기본 사용법
Collections.sort()는 List 인터페이스를 구현한 컬렉션을 정렬할 수 있습니다.
import java.util.*;
public class CollectionsSortExample1 {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(Arrays.asList(5, 2, 9, 1, 3));
Collections.sort(numbers);
System.out.println(numbers);
}
}
실행 결과
[1, 2, 3, 5, 9]
👉 리스트가 오름차순(자연 정렬)으로 정렬되었습니다.
2. 문자열 리스트 정렬
문자열 리스트도 같은 방식으로 정렬할 수 있습니다.
import java.util.*;
public class CollectionsSortExample2 {
public static void main(String[] args) {
List<String> names = new ArrayList<>(Arrays.asList("Tom", "Alice", "Bob", "David"));
Collections.sort(names);
System.out.println(names);
}
}
실행 결과
[Alice, Bob, David, Tom]
👉 문자열은 사전순으로 정렬됩니다.
3. 내림차순 정렬
Collections.reverseOrder()를 함께 사용하면 내림차순 정렬이 가능합니다.
import java.util.*;
public class CollectionsSortExample3 {
public static void main(String[] args) {
List<Integer> numbers = new ArrayList<>(Arrays.asList(5, 2, 9, 1, 3));
Collections.sort(numbers, Collections.reverseOrder());
System.out.println(numbers);
}
}
실행 결과
[9, 5, 3, 2, 1]
4. 사용자 정의 객체 정렬
사용자 정의 클래스도 Comparable이나 Comparator를 구현하면 정렬할 수 있습니다.
import java.util.*;
class Person {
String name;
int age;
Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return name + "(" + age + ")";
}
}
public class CollectionsSortExample4 {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
people.add(new Person("Tom", 25));
people.add(new Person("Alice", 30));
people.add(new Person("Bob", 20));
// 나이순 오름차순 정렬
Collections.sort(people, Comparator.comparingInt(p -> p.age));
System.out.println(people);
}
}
실행 결과
[Bob(20), Tom(25), Alice(30)]
👉 Comparator를 이용해 원하는 기준으로 정렬할 수 있습니다.
5. Arrays.sort() vs Collections.sort()
Arrays.sort()→ 배열(Array)을 정렬Collections.sort()→ 리스트(List)를 정렬- 내부적으로는 둘 다 TimSort 알고리즘을 사용
- 배열과 리스트는 자료구조 차이가 있기 때문에 상황에 맞게 선택해야 함
6. 정리
Collections.sort()는 List 정렬을 위한 메서드- 기본은 오름차순,
Collections.reverseOrder()로 내림차순 가능 Comparator를 이용해 사용자 정의 객체도 정렬 가능Arrays.sort()와는 정렬 대상이 다르다 (배열 vs 리스트)
👉 리스트 정렬은 Collections.sort(), 배열 정렬은 Arrays.sort()!
반응형
'IT > Java' 카테고리의 다른 글
| [Java 기초] Collections.shuffle()로 리스트 무작위 섞기 (0) | 2025.09.02 |
|---|---|
| [Java 기초] Collections.reverse()로 리스트 순서 뒤집기 (0) | 2025.09.02 |
| [Java 기초] Arrays.parallelSort()로 병렬 정렬하기 (1) | 2025.09.01 |
| [Java 기초] Arrays.mismatch()로 배열의 차이점 찾기 (0) | 2025.08.31 |
| [Java 기초] Arrays.deepToString()으로 다차원 배열 출력하기 (0) | 2025.08.31 |
Comments