- 처음 풀이
sort를 사용해서 각 단어의 자리수를 비교
function solution(strings, n) {
let answer = strings.sort((a, b) => {
if(a[n] > b[n]) return 1;
if(a[n] < b[n]) return -1;
if(a[n] === b[n]){
if(a>b) return 1;
if(a<b) return -1;
return 0;
}
});
return answer;
}
- 다른 사람 풀이
localeCompare를 사용했다.
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare
function solution(strings, n) {
// strings 배열
// n 번째 문자열 비교
return strings.sort((s1, s2) => s1[n] === s2[n] ? s1.localeCompare(s2) : s1[n].localeCompare(s2[n]));
}