分享一个今天写的简单的文件名排序
代码语言:javascript复制function fileNameCompare(a, b) {
if (a == null || b == null) return 0;
let na = a.split(/[-_.—, (]/)
let nb = b.split(/[-_.—, (]/)
let maxLoop = Math.max(na.length, nb.length)
for (let i = 0; i < maxLoop; i ) {
if (!isNaN(Number(na[i])) && !isNaN(Number(nb[i]))) {
let num = Number(na[i]) - Number(nb[i])
if (num !== 0) {
return num
}
}
}
let ma = a.match(/[0-9] /)
let mb = b.match(/[0-9] /)
if (ma.length && mb.length) {
let num = Number(ma[0]) - Number(mb[0])
if (num !== 0) {
return num
}
}
return a.localeCompare(b)
}
// 使用:
let arr =
[
{name: '28-01_00.jpg'}
, {name: '28-01_01.jpg'}
, {name: '28-03_01.jpg'}
, {name: '28-03_03.jpg'}
, {name: '28-02_01.jpg'}
, {name: '文件名1 (1).jpg'}
, {name: '文件名10.jpg'}
, {name: '文件名2.jpg'}
, {name: '文件名1.jpg'}
, {name: undefined}
, {name: '文件名1 (1) - 副本.jpg'}
].sort((a, b) => fileNameCompare(a.name, b.name))
console.log(arr)
效果: