-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhyscExamSort.java
More file actions
56 lines (46 loc) · 1.52 KB
/
PhyscExamSort.java
File metadata and controls
56 lines (46 loc) · 1.52 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
package sort;
import java.util.Arrays;
import java.util.Comparator;
// 클래스 객체 배열의 정렬(병합 정렬)
// 자연 정렬이 필요하지 않은 배열의 경우
public class PhyscExamSort {
// 신체검사 데이터
static class PhyscData {
String name;
int height;
double vision;
PhyscData(String name, int height, double vision) {
this.name = name;
this.height = height;
this.vision = vision;
}
public String toString() {
return name + " " + height + " " + vision;
}
// 키 오름차순 comparator
static final Comparator<PhyscData> HEIGHT_ORDER = new HeightOrderComparator();
private static class HeightOrderComparator implements Comparator<PhyscData> {
@Override
public int compare(PhyscData o1, PhyscData o2) {
return (o1.height > o2.height) ? 1 : (o1.height < o2.height) ? -1 : 0;
}
}
}
public static void main(String[] args) {
PhyscData[] x = {
new PhyscData("이나령", 162, 0.3),
new PhyscData("전서현", 173, 0.7),
new PhyscData("이수민", 175, 2.0),
new PhyscData("홍준기", 171, 1.5),
new PhyscData("유지훈", 168, 0.4),
new PhyscData("이호연", 174, 1.2),
new PhyscData("김한결", 169, 0.8)
};
Arrays.sort(x, PhyscData.HEIGHT_ORDER);
System.out.println("< 신체검사 리스트 >");
System.out.println("이름 키 시력");
System.out.println("--------------------");
for(int i=0; i<x.length; i++)
System.out.printf("%-8s%3d%5.1f\n", x[i].name, x[i].height, x[i].vision);
}
}