forked from ndb796/python-for-coding-test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.java
46 lines (35 loc) · 1001 Bytes
/
9.java
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
import java.util.*;
class Fruit implements Comparable<Fruit> {
private String name;
private int score;
public Fruit(String name, int score) {
this.name = name;
this.score = score;
}
public String getName() {
return this.name;
}
public int getScore() {
return this.score;
}
// 정렬 기준은 '점수가 낮은 순서'
@Override
public int compareTo(Fruit other) {
if (this.score < other.score) {
return -1;
}
return 1;
}
}
public class Main {
public static void main(String[] args) {
List<Fruit> fruits = new ArrayList<>();
fruits.add(new Fruit("바나나", 2));
fruits.add(new Fruit("사과", 5));
fruits.add(new Fruit("당근", 3));
Collections.sort(fruits);
for (int i = 0; i < fruits.size(); i++) {
System.out.print("(" + fruits.get(i).getName() + "," + fruits.get(i).getScore() + ") ");
}
}
}