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 random from a scan, so i can never be sure about the order.
0:"js\game\global\app.js"
1:"js\game\global\camera.js"
2:"js\game\global\displayGroups.js"
3:"js\game\global\dataBase.js"
4:"js\game\global\mouse.js"
5:"js\game\global\loaders.js"
6:"js\game\global\stage.js"
7:"js\game\global\polyfill.js"
8:"js\game\scenes\scene_boot.js"
9:"js\game\scenes\scene_IntroVideo.js"
10:"js\game\scenes\scene_loader.js"
11:"js\game\scenes\scene_Map1.js"
12:"js\game\scenes\_scene_base.js" //need to be alway first
13:"js\game\scenes\scene_Title.js"
So in this example, after sorting, the file _scene_base
should be first.
Here is my test code, but it is revealed unstable and I don't know why, sometime it not work.
files.sort((a,b) => {return a.indexOf("\_")>-1?-files.indexOf(a):0 });
sorry if my english are poor guys.
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)