백준 10814번 (나이순 정렬)

김경욱·2025년 8월 29일

백준

목록 보기
69/121

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import java.util.*;

import static java.util.Collections.*;

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());
    String[][] people = new String[N][2];


   for (int i = 0 ; i < N; i++)
    {
        StringTokenizer st = new StringTokenizer(br.readLine());
        people[i][0] = st.nextToken();
        people[i][1] = st.nextToken();
    }

   Arrays.sort(people,(a , b) -> Integer.parseInt(a[0]) - Integer.parseInt(b[0]));


    for (int i = 0 ; i < N; i++)
    {
        System.out.println(people[i][0]+" "+people[i][1]);
    }

















}

}
2차원 배열로 풀릴 지 몰랐다. 처음에는 Map을 쓰고 Map을 list로 바꿀려 했는데 내가 모르는 지식이 너무 많아서 못 풀었다. 이제 람다가 많이 쓰이는 것 같다.
Arrays.sort(people, (a,b -> Integer.parseInt(a[i]) - Integer.parseInt(b[i])); 이게 나이를 기준으로 오름차순을 하는 람다식이다. 자주 나오니 외우면 좋을 것 같다.

0개의 댓글