코딩 테스트/프로그래머스 level1

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

fullfish 2022. 9. 8. 11:42

코드

function solution(strings, n) {
  strings.sort((a, b) => {
    if (a[n] > b[n]) return 1;
    else if (a[n] < b[n]) return -1;
    else if (a > b) return 1;
    else if (a < b) return -1;
    else return 0;
  });
  return strings;
}

sort안에 function을 잘 지정해주면 된다