정규표현식 안쓰고 어떻게 할지 끙끙대다가 결국 썼다..
{head: foo, number: 9, tail: .txt}
function solution(files) {
let arr = []
const pattern = /(\D+)(\d{1,5})/; // 문자열만 , 1 ~ 5 길이의 숫자
files.forEach((curr, index) => {
const sliceStr = pattern.exec(curr);
const head = sliceStr[1].toLowerCase();
const number = sliceStr[2];
const tail = curr.slice(sliceStr[0].length)
arr.push({head, number, tail, index})
})
arr.sort((a, b) => {
if(a.head === b.head && a.number === b.number) {
// 원래 입력에 주어진 순서를 유지
return 0
}
if(a.head === b.head) {
// number 만 비교
return a.number - b.number // 오름차순
}
return a.head > b.head ? 1 : -1
})
return arr.map((curr) => {return files[curr.index]})
}