プログラマ|ファイル名シーケンス
function solution(files) {
let fileWrap = files.map((file, index) => ({ file, index }));//{file:file, index:index}
const compare = (a, b) => {
const reg = /(\D*)([0-9]*)/i;
const A = a.match(reg);//['abd123','abc'.'123']
const B = b.match(reg);
const compareHead = A[1].toLowerCase().localeCompare(B[1].toLowerCase());
const compareNumber = (a, b) => {
if (parseInt(a) > parseInt(b)) return 1;
else if (parseInt(b) > parseInt(a)) return -1;
else return 0
}
return compareHead === 0 ? compareNumber(A[2], B[2]) : compareHead;
}
fileWrap.sort((a, b) => {
let result = compare(a.file, b.file)
return result === 0 ? a.index - b.index : result
})
return fileWrap.map(e => e.file);
}
Reference
この問題について(プログラマ|ファイル名シーケンス), 我々は、より多くの情報をここで見つけました https://velog.io/@gusgk2658/프로그래머스-파일명정렬テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol