자바 정렬 - Comparator, Comparable

2025. 2. 27. 18:22Java/Java 문법

1. 일반 배열, 리스트 정렬

//1. 일반 배열 정렬: Arrays.sort()
int[] intArr = {5, 2, 8, 1, 3};
Arrays.sort(intArr);
System.out.println(Arrays.toString(intArr)); // [1, 2, 3, 5, 8]

Integer[] integerArr = {5, 2, 8, 1, 3};
Arrays.sort(integerArr, Collections.reverseOrder()); //객체 배열만 가능
System.out.println(Arrays.toString(integerArr)); // [8, 5, 3, 2, 1]

//2. 일반 리스트 정렬: Collections.sort()
List<Integer> intArrayList = Arrays.asList(5, 2, 8, 1, 3);
Collections.sort(intArrayList); // [1, 2, 3, 5, 8]
Collections.reverse(intArrayList); // [8, 5, 3, 2, 1]

 

2. Comparable<T>과 Comparator<T>

  Comparable<T> Comparator<T>
비교 기준  자기 자신(this)이 기준을 정해서 비교 외부에서 비교 기준을 따로 정해서 비교
구현 방식 compareTo(T o) 메서 구현  compare(T o1, T o2) 메서드 구현 
적용 방식  객체 자체에 정렬 기준 포함
(ex. String, Integer)
여러 개의 정렬 기준 가능
(ex. 나이순, 이름순 정렬 등)
사용 예시 Collections.sort(list) Collections.sort(list, comparator)

 

3. Comparable<T> 사용 

class Person implements Comparable<Person> {
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public int compareTo(Person other) {
    	//크다(1), 같다(0), 작다(-1)
        return Integer.compare(this.age, other.age); // 나이 순으로 정렬
    }
}

//사용자정의클래스 배열 정렬 
Person[] people = {new Person("Alice", 30), new Person("Bob", 25)};
Arrays.sort(people); // 나이순 정렬 (Comparable 기준)

//사용자정의클래스 리스트 정렬 
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 30));
people.add(new Person("Bob", 25));
Collections.sort(people); // 나이순 정렬

 

 

4. Comparator<T> 사용

4-1. Comparator 클래스 구현 

class Person{
    String name;
    int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

//Person 객체를 비교하기 위한 AgeComparator 클래스 따로 작성 
class AgeComparator implements Comparator<Person> {
    @Override
    public int compare(Person p1, Person p2) {
        return Integer.compare(p1.age, p2.age); // 나이 오름차순
    }
}

//사용자정의클래스 배열 정렬 
Person[] people = {new Person("Alice", 30), new Person("Bob", 25)};
Arrays.sort(people, new AgeComparator()); //나이 기준으로 정렬

//사용자정의클래스 리스트 정렬 
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 30));
people.add(new Person("Bob", 25));
Collections.sort(people, new AgeComparator()); //나이 기준으로 정렬

 

4-2. 익명 클래스 / 람다표현식 사용  

Person[] people = {new Person("Alice", 30), new Person("Bob", 25)};

List<Person> people = new ArrayList<>();
people.add(new Person("철수"), 30);
people.add(new Person("영희"), 34); 

//익명 클래스 사용
Arrays.sort(people, new Comparator<Person>() {
    @Override
    public int compare(Person p1, Person p2) {
        return p1.name.compareTo(p2.name); // 이름순 정렬
    }
});

Collections.sort(people, new Comparator<Person>() {
    @Override
    public int compare(Person p1, Person p2) {
        return Integer.compare(p1.age, p2.age); // 나이 오름차순
    }
});

//람다 표현식 사용 -> 간결해서 선호됨 
Arrays.sort(people, (p1, p2) -> p1.name.compareTo(p2.name)); // 이름순 정렬
people.sort((p1, p2) -> Integer.compare(p1.age, p2.age)); //나이순 정렬