Comparator, Comparator

  1. java.lang.Comparable ์ธํ„ฐํŽ˜์ด์Šค - ์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๊ฐ์ฒด ์Šค์Šค๋กœ์—๊ฒŒ ๋ถ€์—ฌํ•˜๋Š” ํ•œ๊ฐ€์ง€ ๊ธฐ๋ณธ ์ •๋ ฌ ๊ทœ์น™์„ ์„ค์ •ํ•œ๋‹ค

  • Comparable์„ ๊ตฌํ˜„ํ•˜๊ณ  ์žˆ๋Š” ํด๋ž˜์Šค๋“ค์€ ๊ฐ™์€ ํƒ€์ž…์˜ ์ธ์Šคํ„ด์Šค๋ผ๋ฆฌ ์„œ๋กœ ๋น„๊ตํ•  ์ˆ˜ ์žˆ๋Š” ํด๋ž˜์Šค์ด๋‹ค. (wrapper ํด๋ž˜์Šค, String, Date, Fileโ€ฆ). ๊ธฐ๋ณธ ์ •๋ ฌ ๊ธฐ์ค€์„ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐ์— ์‚ฌ์šฉํ•œ๋‹ค.

  • ์ธํ„ฐํŽ˜์ด์Šค ๋‚ด์˜ int compareTo(T o) ๋ฉ”์„œ๋“œ๋Š” ๊ฐ์ฒด ์ž๊ธฐ ์ž์‹ (this)์™€ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ์ฒด o๋ฅผ ๋น„๊ตํ•œ๋‹ค. ๋‘ ๊ฐ์ฒด๊ฐ€ ๊ฐ™์œผ๋ฉด 0, ๋น„๊ตํ•˜๋Š” ๊ฐ’๋ณด๋‹ค ์ž‘์œผ๋ฉด ์Œ์ˆ˜, ํฌ๋ฉด ์–‘์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค

  • ๊ธฐ๋ณธ์ ์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ๋˜๋Š”๋ฐ, ๋‚ด๋ฆผ์ฐจ์ˆœ or ๋‹ค๋ฅธ ๊ธฐ์ค€์— ๋”ฐ๋ผ ์ •๋ ฌํ•˜๊ณ  ์‹ถ์œผ๋ฉด Comparator๋ฅผ ๊ตฌํ˜„ํ•ด์„œ ์ •๋ ฌ๊ธฐ์ค€์„ ์ œ๊ณตํ•  ์ˆ˜ ์žˆ๋‹ค.

class Student implements Comparable<Student> {
		int kor, eng, math;

    public Student(int kor, int eng, int math){
        this.kor = kor;
        this.eng = eng;
        this.math = math;
    }
		
		// base. ๊ตญ์–ด ์ ์ˆ˜ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ๋ฐฉ์‹
		@Override
		public int compareTo(Student student) {
			if(this.kor > student.kor) return 1;
			else if(this.kor > student.kor) return -1;
			return 0;
		}
		
		// // simple. 1๋ณด๋‹ค ๊ฐ„๋‹จํ•œ ๋ฐฉ์‹
		@Override
		public int compareTo(Student student) { 	
			return this.kor - student.kor; // ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค๋ฉด student.kor - this.kor๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋œ๋‹ค
		}
}

// ์‹ค์ „. ๊ฐ์ฒด์˜ ์—ฌ๋Ÿฌ ํ•„๋“œ๊ฐ’์— ๋Œ€ํ•ด์„œ ๊ฐ๊ฐ ๋‹ค๋ฅธ ์ •๋ ฌ๊ทœ์น™์„ ์ ์šฉ
class Student implements Comparable<Student> {
    int kor, eng, math;

    public Student(int kor, int eng, int math){
        this.kor = kor;
        this.eng = eng;
        this.math = math;
    }

    @Override
    public int compareTo(Student student) {
        if(this.kor == student.kor)	     // ๊ตญ์–ด ์ ์ˆ˜๊ฐ€ ์ผ์น˜ํ•œ๋‹ค๋ฉด
            return this.eng - student.eng;  // ์˜์–ด ์ ์ˆ˜๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
        return this.kor - student.kor;      // ๊ตญ์–ด ์ ์ˆ˜๊ฐ€ ๋‹ค๋ฅด๋‹ค๋ฉด, ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌํ•ฉ๋‹ˆ๋‹ค.
    }
};
  1. java.util.Comparator ์ธํ„ฐํŽ˜์ด์Šค - ์ •๋ ฌ ๋Œ€์ƒ ํด๋ž˜์Šค์˜ ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ์ˆ˜์ •ํ•˜์ง€ ๋ชปํ•  ๊ฒฝ์šฐ ์‚ฌ์šฉํ•œ๋‹ค.

  • ๊ธฐ๋ณธ ์ •๋ ฌ ๊ธฐ์ค€ ์™ธ์˜ ๋‹ค๋ฅธ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌํ•˜๊ณ ์ž ํ•  ๋•Œ ์‚ฌ์šฉ

  • Comparator ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ฒด๋ฅผ Arrays.sort()๋‚˜ Collections.sort() ๊ฐ™์€ ์ •๋ ฌ ๋ฉ”์„œ๋“œ์˜ ์ถ”๊ฐ€ ์ธ์ž๋กœ ๋„˜๊ธด๋‹ค.

  • ์ธํ„ฐํŽ˜์ด์Šค ๋‚ด์˜ int compare(T o1, T o2) ๋ฉ”์„œ๋“œ๋Š” ๋‘ ๋งค๊ฐœ๋ณ€์ˆ˜ ๊ฐ์ฒด๋ฅผ ๋น„๊ตํ•œ๋‹ค.

  • Comparator๋ฅผ ์ต๋ช… ํด๋ž˜์Šค, ๋žŒ๋‹คํ‘œํ˜„์‹์œผ๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค.

  • Stream์œผ๋กœ ์ปฌ๋ ‰์…˜์ด๋‚˜ ๋ฐฐ์—ด์„ ์ฝ๊ณ  sorted() ๋ฉ”์„œ๋“œ๋กœ Comparator ๊ตฌํ˜„์ฒด๋ฅผ ๋ฐ›์•„์„œ ์ •๋ ฌํ•œ ๋’ค ์ƒˆ๋กœ์šด ๋ฆฌ์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

// ๋ฐฉ๋ฒ•2. comparator ์ธํ„ฐํŽ˜์ด์Šค ๊ตฌํ˜„ - ๋žŒ๋‹คํ•จ์ˆ˜๋กœ ๋Œ€์ฒดํ•  ์ˆ˜ ์žˆ๋‹ค. 
Arrays.sort(student, (a, b) -> a.kor - b.kor);

// String ๋‚ด์— ์ •์˜๋œ ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ์—†์ด ์‚ฌ์ „์ˆœ ์ •๋ ฌํ•˜๋Š” Comparator
Arrays.sort(strArr, String.CASE_INSENSITIVE_ORDER);

Arrays.sort(students, new Comparator<Student>() {
	@Override
	public int compare(Student a, Student b) { // base.
		return a.kor - b.kor;
	}

	@Override
	public int compare(Object o1, Object o2) { // Comparable ๊ตฌํ˜„ํ•˜์ง€ ์•Š์€ ๊ฐ์ฒด๋ฅผ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌ
			if(o1 instanceof Comparable && o2 instanceof Comparable) {
				Comparable c1 = (Comparable)o1;
				Comparable c2 = (Comparable)o2;
				return c1.compareTo(c2) * -1; // ๊ธฐ๋ณธ ์ •๋ ฌ๋ฐฉ์‹์˜ ์—ญ์œผ๋กœ ๋ณ€๊ฒฝ
			}
			return -1;
	}
});

// Stream์œผ๋กœ ์ƒˆ๋กœ์šด ์ •๋ ฌ๋œ ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฐฉ์‹
// Stream ํด๋ž˜์Šค์˜ sorted() ๋ฉ”์„œ๋“œ๋Š” Comparator ๊ฐ์ฒด๋ฅผ ์ธ์ž๋กœ ๋ฐ›์•„์„œ ์ •๋ ฌ๊ธฐ์ค€์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. 
List<Student> sortedList = list.stream().sorted((a, b) -> b.kor - a.kor).collect(Collectors.toList());

Last updated