In my view, the most effective strategy for this particular case is the recommendation made by commonpike. To enhance it for modern browsers, I would propose the following adjustment:
// aao is the "associative array" you want to "sort"
Object.keys(aao).sort(function(a,b){return aao[b]-aao[a]});
This method can be easily implemented and perform exceptionally well in this specific scenario. Here's an example of how you can utilize it:
let aoo={};
aao["sub2"]=1;
aao["sub0"]=-1;
aao["sub1"]=0;
aao["sub3"]=1;
aao["sub4"]=0;
let sk=Object.keys(aao).sort(function(a,b){return aao[b]-aao[a]});
// loop through the sorted keys in `sk` to perform tasks
for (let i=sk.length-1;i>=0;--i){
// manipulate sk[i] or aoo[sk[i]]
}
In addition, I have included a more "generic" function below that allows for sorting in a wider range of situations. It combines my suggested improvement with the methods from answers provided by Ben Blank and PopeJohnPaulII, enabling you to choose between ascending and descending order:
// aao := the "associative array" you wish to "sort"
// comp := the "field" to compare or "" if no "fields" and only need to compare values
// intVal := false if comparing non-integer values
// desc := set to true to sort keys in descending order (default is ascending)
function sortedKeys(aao,comp="",intVal=false,desc=false){
let keys=Object.keys(aao);
if (comp!="") {
if (intVal) {
if (desc) return keys.sort(function(a,b){return aao[b][comp]-aao[a][comp]});
else return keys.sort(function(a,b){return aao[a][comp]-aao[a][comp]});
} else {
if (desc) return keys.sort(function(a,b){return aao[b][comp]<aao[a][comp]?1:aao[b][comp]>aao[a][comp]?-1:0});
else return keys.sort(function(a,b){return aao[a][comp]<aao[b][comp]?1:aao[a][comp]>aao[b][comp]?-1:0});
}
} else {
if (intVal) {
if (desc) return keys.sort(function(a,b){return aao[b]-aao[a]});
else return keys.sort(function(a,b){return aao[a]-aao[b]});
} else {
if (desc) return keys.sort(function(a,b){return aao[b]<aao[a]?1:aao[b]>aao[a]?-1:0});
else return keys.sort(function(a,b){return aao[a]<aao[b]?1:aao[a]>aao[b]?-1:0});
}
}
}
You can experiment with the functionality using the code snippets below:
let items={};
items['Edward']=21;
items['Sharpe']=37;
items['And']=45;
items['The']=-12;
items['Magnetic']=13;
items['Zeros']=37;
console.log("1: "+sortedKeys(items));
console.log("2: "+sortedKeys(items,"",false,true));
console.log("3: "+sortedKeys(items,"",true,false));
console.log("4: "+sortedKeys(items,"",true,true));
items={};
items['k1']={name:'Edward',value:21};
items['k2']={name:'Sharpe',value:37};
items['k3']={name:'And',value:45};
items['k4']={name:'The',value:-12};
items['k5']={name:'Magnetic',value:13};
items['k6']={name:'Zeros',value:37};
console.log("1: "+sortedKeys(items,"name"));
console.log("2: "+sortedKeys(items,"name",false,true));
Feel free to loop through the sorted keys using the below code:
let sk=sortedKeys(aoo);
// loop through the sorted keys in `sk` to perform tasks
for (let i=sk.length-1;i>=0;--i){
// manipulate sk[i] or aoo[sk[i]]
}
Finally, I have included some helpful references to Object.keys and Array.sort