본문 바로가기
알고리즘/남궁성 자바 1000제

[Java1000제] 성적처리 3 - Comparator를 이용한 정렬

by Love of fate 2021. 2. 12.
728x90
반응형

[문제3] list를 다양한 기준으로 정렬하기 위해 Comaprator를 구현한 클래스를 구현하세요.

           ClassTotalComparator - 반별로 총점이 높은 순에서 낮은 순으로 정렬(반은 오름차순, 총점은 내림차순)

           ClassStudentNo - 반, 번호 순으로 내림차순 정렬



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package testAl;
 
import java.util.*;
 
class SungJukEx3 {
      public static void main(String[] args) {
            ArrayList<Student2> list = new ArrayList<Student2>(); 
 
             // 이름, 반, 번호, 국어, 수학, 영어
            list.add(new Student2("남궁성"3,2,100,100,100));
            list.add(new Student2("왕자바"3,1,90,100,80));
            list.add(new Student2("자바왕"3,3,70,100,100));
            list.add(new Student2("킹왕짱"1,2,100,60,90));
            list.add(new Student2("자바짱"1,1,100,100,100));
            list.add(new Student2("최고수"1,3,100,80,60));
            list.add(new Student2("홍길동"2,1,50,80,100));
            list.add(new Student2("일지매"2,3,70,80,100));
            list.add(new Student2("변강쇠"2,4,80,80,85));
            list.add(new Student2("이원구"2,2,90,90,90));
 
            System.out.println("[반별 총점높은 순으로 정렬]");
            Collections.sort(list, new ClassTotalComparator()); // 반, 총점 순으로 정렬
            printList(list);
 
            System.out.println();
 
            System.out.println("[반, 번호 순으로 정렬]");
            Collections.sort(list, new ClassStudentNoComparator()); // 반, 번호 순으로 정렬
            printList(list);
      }
 
      public static void printList(List<Student2> list) {
            System.out.println("이름\t반\t번호\t국어\t수학\t영어\t총점 ");
            System.out.println("====================================================");
 
            for(Student2 s : list) {
                  System.out.println(s);
            }
 
            System.out.println("====================================================");
      }
}
 
class Student2 implements Comparable<Student2> {
    
      String name = "";
      int classNo = 0;
      int studentNo = 0;
      int koreanScore = 0;
      int mathScore = 0;
      int englishScore = 0;
 
      int total = 0;
 
      Student2(String name, int classNo, int studentNo, 
              int koreanScore, int mathScore, int englishScore) {
            this.name = name;
            this.classNo = classNo;
            this.studentNo = studentNo;
            this.koreanScore = koreanScore;
            this.mathScore = mathScore;
            this.englishScore = englishScore;
 
            total = koreanScore + mathScore + englishScore;
      }
 
      public String toString() {
            return name + "\t"
                  + classNo + "\t"
                  + studentNo + "\t"
                  + koreanScore + "\t"
                  + mathScore + "\t"
                  + englishScore + "\t"
                  + total + "\t";
      }
 
      public int compareTo(Student2 obj) {
            return obj.total - this.total;
//             return this.name.compareTo(obj.name); // 이름기준으로 오름차순 정렬
      }
 
/* 제네릭스(Generics) 사용하지 않은 버젼
      public int compareTo(Object obj) {
            int result = -1;
 
            if(obj instanceof Student) {
                  Student tmp = (Student)obj;
 
                  result = tmp.total - this.total;
            }
 
            return result;
      }
*/
// end of class Student
 
class ClassTotalComparator implements Comparator<Student2> {
      public int compare(Student2 s1, Student2 s2) { 
          
        int result = s1.classNo - s2.classNo;
        
        if(result == 0) {
            result = s2.total - s1.total; 
        }
 
        return result;
      }
}
 
class ClassStudentNoComparator implements Comparator<Student2> { 
      public int compare(Student2 s1, Student2 s2) { 
          
         int result = s1.classNo - s2.classNo; //오름차순으로 정렬
 
         if(result == 0) { //같으면 같은 반이면 번호로 름차순으로 정렬
             result = s1.studentNo - s2.studentNo;
         }
                  
         return result;
      }
}
 

 

[출력 결과]

 

SungJukEx3.java
0.00MB

 

// 정렬이 어떻게 되는 건지 잘 이해가 가질 않는다. 

// 내용을 좀 더 살펴볼 필요가 있을 것 같다. 

// 이해만 한다면 엄청 쉬운 문제 

 

728x90
반응형