백준 10814번 node.js 해결
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const count = Number(input[0]);
const arr = [];
for(let i = 1 ; i <= count ; i += 1 ){
const user = input[i].split(' ');
arr.push([Number(user[0]), user[1]]);
}
//sort()는 stable sort이므로 같은 값 비교시 위치 변동 없음
arr.sort((a, b) => a[0] - b[0]);
let result = '';
for(let j = 0 ; j < count ; j += 1){
result += arr[j][0] + ' '+ arr[j][1] + '\n';
}
console.log(result);
이름과 나이를 객체로 묶습니다. 배열도 객체입니다.
나이가 같으면 가입한 순서로 정렬된다는 것은 위치가 들어온 그대로 유지된다는 뜻입니다. 이것을 stable sort라고 하는데요.
자바스크립트의 sort()는 기본적으로 stable sort 제공합니다. 풀고 난 다음에 알게 되어 바꿔서 재채점도 했네요ㅎㅎ