자바 배열을 정렬하는 Comparable 관련하여 질문드립니다. 아래 소스 보시면 compareTo를 오버라이딩하면서 this.total-s.total 을 return 값으로 주는데요 this.total은 계속 0일테고 0-s.total을 계속 해봤자 어차피 값은 계속 300, 200, 400, 100, 700 으로 동일할텐데 굳이 저렇게 쓰는 이유가 있을까요? 그리고 값이 아니라 부호가 중요한것이라면 다 결과값은 어차피 다 플러스 일텐데 어떻게 정렬처리가 되는것인지 궁금합니다. 마지막으로 compare(Object o1, Object o2)는 compareTo와 어떤차이가 있는지 궁금합니다. 어차피 같은 정렬인데 두개로 나누어 놓은건가요? ``` public class Student implements Comparable { int total = 0; String name = ""; Student(int total, String name) { this.total = total; this.name = name; } @Override public String toString() { // TODO Auto-generated method stub return "total : " + total + ", " + "name : " + name; } @Override public int compareTo(Object o) { Student s = (Student) o; return this.total - s.total; } public static void main(String[] args) { Student[] s = { new Student(300, "김자바"), new Student(200, "이자바"), new Student(400, "강자바"), new Student(100, "권자바"), new Student(700, "유자바") }; Arrays.sort(s); System.out.println(Arrays.toString(s)); } } ```
# 해당 링크를 참조해보세요 - https://cloudstudying.kr/questions/6 - https://developer88.tistory.com/75 ## PS this.total은 객체 생성시 입력한 값에 의해 매번 바뀝니다. ``` new Student(300, "김자바"); // this.total => 300 new Student(200, "이자바"); // this.total => 200 ... ```