[백준] 10814 나이순정렬
예전에 시도했다가 실패한 문제라서 다시 풀어봤다
Comparator 쓰는법 빨랑 외워버려야지..!!
import java.util.*;
import java.io.*;
// 회원 정보가 담길 클래스
class Member{
int i;
int age;
String name;
Member(int i, int age, String name){
this.i = i;
this.age = age;
this.name = name;
}
}
public class Main{
public static void main(String []args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
ArrayList<Member> list = new ArrayList<>();
for(int i = 0 ; i < n ; i++ ){
StringTokenizer st = new StringTokenizer(br.readLine());
list.add(new Member(i, Integer.parseInt(st.nextToken()), st.nextToken()));
}
// 정렬해준다
list.sort(new Comparator<Member>(){
@Override
public int compare(Member o1, Member o2){
if(o1.age < o2.age){
return -1;
}else if(o1.age == o2.age){
return o1.i - o2.i;
}else{
return 1;
}
}
});
// 출력
StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < list.size() ; i++ ){
sb.append(list.get(i).age + " " + list.get(i).name + "\n");
}
System.out.println(sb);
}
}
Comments