Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Sort by Extension by vvm70
import assert from "assert";
function sortByExt(files: string[]): string[] {
let set1 = files.filter(x => [-1, 0, x.length - 1].includes(x.lastIndexOf('.'))).sort();
let set2 = Array.from(new Set(files.filter(x => !new Set(set1).has(x)))).sort().
sort((a, b) => {
a = a.slice(a.lastIndexOf('.') + 1)
b = b.slice(b.lastIndexOf('.') + 1)
if(a < b)
return -1;
if(a > b)
return 1;
return 0;
});
return [...set1, ...set2];
}
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', 'file']), ['file', '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!");
Aug. 5, 2020
Comments: