Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Sort by extension solution in Clear category for Sort by Extension by Alex_Yanul
import assert from "assert";
const regexp = /(.*)\.(\w*)$/;
function sortByExt(files) {
files.sort((o1, o2) => {
const [, name1, ext1] = o1.match(regexp);
const [, name2, ext2] = o2.match(regexp);
const compareExt = ext1.localeCompare(ext2);
const compareName = name1.localeCompare(name2);
return name1 === "" || name2 === "" ? compareName : compareExt;
})
return files;
}
console.log('Example:');
console.log(sortByExt(["no.name.","green.bat","345.bin","format.c",".config","my.doc","1.exe","best.test.exe"]));
assert.deepEqual(sortByExt(["no.name.","green.bat","345.bin","format.c",".config","my.doc","1.exe","best.test.exe"]), [".config","no.name.","green.bat","345.bin","format.c","my.doc","1.exe","best.test.exe"]);
assert.deepEqual(sortByExt(['1.cad', '1.bat', '1.aa']), ['1.aa', '1.bat', '1.cad']);
assert.deepEqual(sortByExt(['1.cad', '1.bat', '1.aa', '2.bat']), ['1.aa', '1.bat', '2.bat', '1.cad']);
assert.deepEqual(sortByExt(['1.cad', '1.bat', '1.aa', '.bat']), ['.bat', '1.aa', '1.bat', '1.cad']);
assert.deepEqual(sortByExt(['1.cad', '1.bat', '.aa', '.bat']), ['.aa', '.bat', '1.bat', '1.cad']);
assert.deepEqual(sortByExt(['1.cad', '1.', '1.aa']), ['1.', '1.aa', '1.cad']);
assert.deepEqual(sortByExt(['1.cad', '1.bat', '1.aa', '1.aa.doc']), ['1.aa', '1.bat', '1.cad', '1.aa.doc']);
assert.deepEqual(sortByExt(['1.cad', '1.bat', '1.aa', '.aa.doc']), ['1.aa', '1.bat', '1.cad', '.aa.doc']);
console.log("Coding complete? Click 'Check' to earn cool rewards!");
June 1, 2021