Sort Array String Which Begins With A Letter
How i can sort this array list to be sure all files starting with _ will be first? Context: is for a loader and i want to be sure to load all files with _ first. List are generate
Solution 1:
Another solution based on indexOf
and only the first position of the underscore.
const arr = [
'js\\game\\global\\app.js',
'js\\game\\global\\camera.js',
'js\\game\\global\\displayGroups.js',
'js\\game\\global\\dataBase.js',
'js\\game\\global\\mouse.js',
'js\\game\\global\\loaders.js',
'js\\game\\global\\stage.js',
'js\\game\\global\\polyfill.js',
'js\\game\\scenes\\scene_boot.js',
'js\\game\\scenes\\scene_IntroVideo.js',
'js\\game\\scenes\\scene_loader.js',
'js\\game\\scenes\\scene_Map1.js',
'js\\game\\scenes\\scene_Title.js',
'js\\game\\scenes\\_scene_base.js',
'js\\game\\scenes\\scene_Loader.js',
'js\\game\\scenes\\_scenebase.js',
'js\\game\\scenes\\sceneTit_le.js',
'js\\game\\scenes\\scene_introVide_o.js',
'js\\game\\scenes\\sceneIntroVi_deo.js',
];
arr.sort((a, b) => {
const ai = a.indexOf('_');
const bi = b.indexOf('_');
return (ai > -1 && bi > -1 && (ai - bi)) || -1;
});
console.log(arr)
Solution 2:
If you want to place any file that starts with underscore first in order, filter and concatenate it.
const arr = ['js\\game\\global\\app.js',
'js\\game\\global\\camera.js',
'js\\game\\global\\displayGroups.js',
'js\\game\\global\\dataBase.js',
'js\\game\\global\\mouse.js',
'js\\game\\global\\loaders.js',
'js\\game\\global\\stage.js',
'js\\game\\global\\polyfill.js',
'js\\game\\scenes\\scene_boot.js',
'js\\game\\scenes\\scene_IntroVideo.js',
'js\\game\\scenes\\scene_loader.js',
'js\\game\\scenes\\scene_Map1.js',
'js\\game\\scenes\\_scene_base.js',
'js\\game\\scenes\\scene_Title.js'
]
const _files = arr.filter(e => e.indexOf('\\_') !== -1).sort()
const files = arr.filter(e => e.indexOf('\\_') === -1).sort()
const arr2 = _files.concat(files)
console.log(arr2)
Solution 3:
You could simply replace underscore for sorting and get this strings on top of the directory.
var array = ["js\\game\\global\\app.js", "js\\game\\global\\camera.js", "js\\game\\global\\displayGroups.js", "js\\game\\global\\dataBase.js", "js\\game\\global\\mouse.js", "js\\game\\global\\loaders.js", "js\\game\\global\\stage.js", "js\\game\\global\\polyfill.js", "js\\game\\scenes\\scene_boot.js", "js\\game\\scenes\\scene_IntroVideo.js", "js\\game\\scenes\\scene_loader.js", "js\\game\\scenes\\scene_Map1.js", "js\\game\\scenes\\_scene_base.js", "js\\game\\scenes\\scene_Title.js"]
array.sort((a, b) => a.replace(/_/g, ' ').localeCompare(b.replace(/_/g, ' ')));
console.log(array);
.as-console-wrapper { max-height: 100%!important; top: 0; }
Solution 4:
Simply use Array.sort()
passing in your own custom comparator and use String.startWith()
to check whether a filename starts with "_" or not:
Try the following:
let arr =['js\\game\\global\\app.js', 'js\\game\\global\\camera.js', 'js\\game\\global\\displayGroups.js', 'js\\game\\global\\dataBase.js', 'js\\game\\global\\mouse.js', 'js\\game\\global\\loaders.js', 'js\\game\\global\\stage.js', 'js\\game\\global\\polyfill.js', 'js\\game\\scenes\\scene_boot.js', 'js\\game\\scenes\\scene_IntroVideo.js', 'js\\game\\scenes\\scene_loader.js', 'js\\game\\scenes\\scene_Map1.js', 'js\\game\\scenes\\_scene_base.js', 'js\\game\\scenes\\scene_Title.js' ];
arr.sort((a,b)=>{
let str1Array = a.split("\\");
let str2Array = b.split("\\");
return str2Array[str2Array.length-1].startsWith("_") -str1Array[str1Array.length-1].startsWith("_")
});
console.log(arr);
Post a Comment for "Sort Array String Which Begins With A Letter"