Comparator, 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; // ๊ตญ์ด ์ ์๊ฐ ๋ค๋ฅด๋ค๋ฉด, ์ค๋ฆ์ฐจ์ ์ ๋ ฌํฉ๋๋ค.
}
};Last updated