Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Sort by Extension solution in Uncategorized category for Sort by Extension by Elena_Korljukova
import assert from "assert";
function sortByExt(files) {
let arr = [];
for (let x of files) {
let a = x.split(/[\.]/g).filter(i => i.length > 0);
let b = '';
if (a.length >= 2 && !x.endsWith('.'))
b = a[a.length - 1];
arr.push([b, x.replace(b, '')]);
}
return arr.sort().map(x => x[1].concat(x[0]));
}
console.log('Example:');
console.log(sortByExt(['1.cad', '1.bat', '1.aa']));
// These "asserts" are used for self-checking
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!");
Oct. 24, 2020