-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilenameSort.js
31 lines (25 loc) · 877 Bytes
/
FilenameSort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
function solution(files) {
var parsedFiles = [];
files.forEach(file => {
const match = file.match(/^([a-zA-Z\s.-]+)(\d+)(.*)$/);
if (match) {
const HEAD = match[1];
const NUMBER = match[2];
const TAIL = match[3];
parsedFiles.push({ HEAD, NUMBER, TAIL, original: file });
}
});
parsedFiles.sort((a, b) => {
const headA = a.HEAD.toLowerCase();
const headB = b.HEAD.toLowerCase();
if (headA < headB) return -1;
if (headA > headB) return 1;
const numberA = parseInt(a.NUMBER, 10);
const numberB = parseInt(b.NUMBER, 10);
if (numberA < numberB) return -1;
if (numberA > numberB) return 1;
return 0;
});
return parsedFiles.map(file => file.original);
}
console.log(solution(["foo010bar020.zip", "foo9.txt"]));