Java에서 정렬 기준을 정의하는 방법은 두 가지.
Comparable 인터페이스 (Comparable<T>)compareTo() 메서드를 구현해 정렬 기준을 정의.Comparator 인터페이스 (Comparator<T>)자바의 정렬 메커니즘은 객체 간의 우선순위를 비교하여 결정하는 방식으로 작동.

| 항목 | Comparable | Comparator |
|---|---|---|
| 비교 위치 | 클래스 내부 | 외부에서 정의 |
| 코드 유지보수 | 클래스 수정 필요 | 외부에서 유연하게 변경 가능 |
| 다중 정렬 기준 | 불가능 (1개만 정의) | 가능 (여러 Comparator 조합 가능) |
| 성능 | 빠름 (직접 구현, 메서드 호출 적음) | 약간 느림 (람다/익명 클래스 오버헤드) |
| 재사용성 | 낮음 | 높음 |
문제 요구 조건:
static class Numbers implements Comparable<Numbers> {
int value;
Numbers(int value) {
this.value = value;
}
public int compareTo(Numbers other) {
if (Math.abs(value) == Math.abs(other.value)) {
return Integer.compare(value, other.value);
} else {
return Integer.compare(Math.abs(value), Math.abs(other.value));
}
}
}
PriorityQueue<Integer> pq =
new PriorityQueue<>((x, y) -> {
return Math.abs(x) == Math.abs(y)
? (x > y ? 1 : -1)
: (Math.abs(x) > Math.abs(y) ? 1 : -1);
});


처음에는 람다의 편의성과 큰 차이점을 못느껴서 항상 Comparator를 사용했었지만, Comparable를 사용한 정렬방식이 훨씬 속도가 빠르다는 점을 알게 되어 추후에는 Comparable를 구현한 정렬방식을 더 많이 사용할 것 같다.