문자열 내 마음대로 정렬하기

hapwoo·2022년 8월 16일
0

Coding test

목록 보기
6/18

https://school.programmers.co.kr/learn/courses/30/lessons/12915

function solution(strings, n) {
    strings.sort((a, b)=>{
        if (a.slice(n,n+1) > b.slice(n,n+1)) {
            return 1
        } else if (a.slice(n,n+1) < b.slice(n,n+1)) {
            return -1
        } else {
            if (a>b) {
                return 1
            } else if (a<b) {
                return -1
            } else {
                return 0
            }
        }
    })
    return strings
}
function solution(strings, n) {
    return strings.sort((s1, s2) => s1[n] === s2[n] ? s1.localeCompare(s2) : s1[n].localeCompare(s2[n]));
}
profile
프론트 개발자

0개의 댓글