-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySortTest5.java
More file actions
70 lines (55 loc) · 2.19 KB
/
ArraySortTest5.java
File metadata and controls
70 lines (55 loc) · 2.19 KB
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
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class ArraySortTest5 {
private static class Person{
private String name;
private int age;
private String job;
private int score;
public Person() {
}
public Person(String name, int age, String job, int score) {
super();
this.name = name;
this.age = age;
this.job = job;
this.score = score;
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("Person [name=");
builder.append(name);
builder.append(", age=");
builder.append(age);
builder.append(", job=");
builder.append(job);
builder.append(", score=");
builder.append(score);
builder.append("]");
return builder.toString();
}
}
public static void main(String[] args) {
ArrayList<Person> list = new ArrayList<>();
list.add(new Person("갓용수", 11, "학생", 100));
list.add(new Person("갓민권", 15, "학생", 90));
list.add(new Person("깡승현", 12, "학생", 76));
list.add(new Person("킹나영", 10, "학생", 80));
list.add(new Person("황태", 12, "학생", 85));
// Arrays.sort(list); //Arrays클래스의 sort메소드를 통해 Collection(List)의 정렬을 할 수 없음~!!
// Collections.sort(list); //Comparable일 경우
// Collections.sort(list,new Person()); //Comparator일 경우 Collections.sort(list, Comparator객체);
//나는 특정인터페이스를 상속받지 않는 Person을 사용하고 싶다~!!
// Collections.sort(list,new Comparator<Person>() {
// @Override
// public int compare(Person o1, Person o2) {
// return o1.age-o2.age;
// }
// });//익명의 내부클래스
//문제) Person을 age오름차순으로 하되 같은 age의 경우 score내림차순으로 정렬하시오.
Collections.sort(list, (o1, o2) -> o2.score - o1.score);//익명의 내부클래스
System.out.println(list);
}// main
}