回答編集履歴

2

compareの実装変更

2020/04/23 11:58

投稿

momon-ga
momon-ga

スコア4820

test CHANGED
@@ -40,11 +40,11 @@
40
40
 
41
41
  public int compare(Score o1, Score o2) {
42
42
 
43
- int s1 = o1.getTotalScore();
43
+ // 比較対象を逆にする
44
44
 
45
- int s2 = o2.getTotalScore();
45
+ Integer s2 = o2.getTotalScore();
46
46
 
47
- return s1 == s2 ? 0 : s1 > s2 ? -1 : 1;
47
+ return s2.compareTo(o1.getTotalScore());
48
48
 
49
49
  }
50
50
 

1

applyAsIntの実装変更

2020/04/23 11:58

投稿

momon-ga
momon-ga

スコア4820

test CHANGED
@@ -28,7 +28,7 @@
28
28
 
29
29
  Listのsortメソッドを、そのまま使う。
30
30
 
31
- Comparetorは昇順なので、判定を逆にする
31
+ Comparetorは昇順なので、判定を逆に実装するのが楽かな
32
32
 
33
33
 
34
34
 
@@ -54,11 +54,7 @@
54
54
 
55
55
  ```
56
56
 
57
- comapreの実装が通常と異なるのがイヤであればCollectionsreverseを使うことが前提
57
+ comapreの実装が通常と異なるのがイヤであれば以下実装も可能です。
58
-
59
- 以下の実装でも可能です。
60
-
61
- ※効率的が処理効率であるなら、revserをかます分こちらの方が重いです。
62
58
 
63
59
 
64
60
 
@@ -74,15 +70,11 @@
74
70
 
75
71
  public int applyAsInt(Score s) {
76
72
 
77
- return s.getBiology() + s.getEnglish() + s.getPhysics();
73
+ return -1 * (s.getBiology() + s.getEnglish() + s.getPhysics());
78
74
 
79
75
  }
80
76
 
81
77
  }));
82
-
83
- // ここで逆順(降順)にする
84
-
85
- Collections.reverse(list);
86
78
 
87
79
  ```
88
80