``` package Test; import java.util.Arrays; class Employee { String name; int[] hours; Employee(String name, int[] hours) { this.name = name; this.hours = hours; Arrays.sort(hours); } int totalHours() { int sum=0; for(int i=0; i<hours.length; i++) { sum = sum+hours[i]; System.out.print(hours[i]+" "); } return sum; } public static void main(String[] args) { Employee []em = new Employee[8]; int []hours0 = {2,4,3,4,5,8,8}; int []hours1 = {7,3,4,3,3,4,4}; int []hours2 = {3,3,4,3,3,2,2}; int []hours3 = {9,3,4,7,3,4,1}; int []hours4 = {3,5,4,3,6,3,8}; int []hours5 = {3,4,4,6,3,4,4}; int []hours6 = {3,7,4,8,3,8,4}; int []hours7 = {6,3,5,9,2,7,9}; em[0] = new Employee("Employee0", hours0); em[1] = new Employee("Employee1", hours1); em[2] = new Employee("Employee2", hours2); em[3] = new Employee("Employee3", hours3); em[4] = new Employee("Employee4", hours4); em[5] = new Employee("Employee5", hours5); em[6] = new Employee("Employee6", hours6); em[7] = new Employee("Employee7", hours7); for(int i=0; i<em.length; i++) { // System.out.println(em[i].totalHours()); em[i].printTotalHours(); } } void printTotalHours() { System.out.printf("%s -> %d 시간\n", name, totalHours()); } // public String toString() { // // return String.format("%s --> %d 시간",name,totalHours()); // } } ```
① for문을 사용해 정렬하는 방법 ``` class Employee { // ... 기존 Employee 클래스 정의 ... public static void main(String[] args) { Employee[] em = new Employee[8]; // 각 직원의 근무 시간 초기화 // ... // 버블 정렬로 직원 배열 정렬 for (int i = 0; i < em.length - 1; i++) { for (int j = 0; j < em.length - i - 1; j++) { if (em[j].totalHours() < em[j + 1].totalHours()) { // 인접한 두 직원의 총 근무 시간을 비교하여 필요한 경우 위치를 교환 Employee temp = em[j]; em[j] = em[j + 1]; em[j + 1] = temp; } } } // 정렬된 직원 정보 출력 for (Employee e : em) { System.out.printf("%s -> %d 시간\n", e.name, e.totalHours()); } } } ``` ② Arrays.sort와 람다식을 이용한 방법 ``` import java.util.Arrays; class Employee { String name; int[] hours; Employee(String name, int[] hours) { this.name = name; this.hours = hours; } int totalHours() { int sum = 0; for (int hour : hours) { sum += hour; } return sum; } public static void main(String[] args) { Employee[] em = new Employee[8]; // 각 직원의 근무 시간 초기화 // ... // 직원 배열을 총 근무 시간에 따라 정렬 Arrays.sort(em, (e1, e2) -> e2.totalHours() - e1.totalHours()); // 정렬된 직원 정보 출력 for (Employee e : em) { System.out.printf("%s -> %d 시간\n", e.name, e.totalHours()); } } } ```