https://www.acmicpc.net/problem/10814
객체 정렬기준 설정
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int numOfCase = Integer.parseInt(br.readLine());
List<Member> list = new ArrayList<>();
StringTokenizer st;
for(int i=0; i<numOfCase; i++){
st = new StringTokenizer(br.readLine());
int age = Integer.parseInt(st.nextToken());
String name = st.nextToken();
list.add(new Member(age, name));
}
Collections.sort(list, (Member member1, Member member2)-> {
for(int i=0; i<list.size()-1; i++){
if(member1.age == member2.age){
return member1.index - member2.index;
}
}
return member1.age - member2.age;
});
StringBuilder sb = new StringBuilder();
for(int i=0; i<list.size(); i++){
sb.append(list.get(i).age).append(" ").append(list.get(i).name).append("\n");
}
System.out.println(sb);
}
}
class Member{
int index = 0;
int age;
String name;
public Member() {}
public Member(int age, String name) {
this.age = age;
this.name = name;
this.index++;
}
}
'알고리즘 > 백준' 카테고리의 다른 글
백준 15649번 - N과 M (1) (Java 8) (0) | 2022.03.05 |
---|---|
백준 18870번 - 좌표 압축 (Java 8) (0) | 2022.03.05 |
백준 1181번 - 단어 정렬 (Java 8) (0) | 2022.03.04 |
백준 11651번 - 좌표 정렬하기 2 (Java 8) (0) | 2022.03.04 |
백준 11650번 - 좌표 정렬하기 (Java 8) (0) | 2022.03.04 |
댓글