function updateUld(pUldValues){
if (pUldValues != null){
pUldValues.sort(compareSort);
}
}
function compareSort (first,second){
var ret = 0;
var uldA = first[1]; //TOTO
var UldB = second[1]; //MAMA
if (uldA.substring(0,1) == "T"){
ret = compareRefUldRefUldTULD(uldA, UldB);
}
if (uldA.substring(0,1) == "M" ||
uldA.substring(0,1) == "A"){
ret = compareRefUldRefUldMix(uldA, UldB);
}
if (uldA.substring(0,1) == "L"){
ret = compareRefUldRefUldLoose(uldA, UldB);
}
if(ret==0){
if (UldB.substring(0,1) == "M" ||
UldB.substring(0,1) == "A"||
UldB.substring(0,1) == "T" ||
UldB.substring(0,1) == "L"){
ret = -1 ;
}else{
ret= UldB.localeCompare(uldA);
}
}
return -ret ;
}
排序前的 pUldValues
包含555 : MAMA + 556 : TOTO
...
排序后: 0 : TOTO + 1 : MAMA
...
我希望我有相同的排序,但格式为: 555 : TOTO + 556 : MAMA
答案 0 :(得分:1)
您不能将数组用作地图,也不能订购对象。
但是从ECMAScript 2015开始,您可以使用 Map 对象来保证密钥的顺序。 See the Map documentation
这里有一个快速例子,输入中有一个对象:
var myMap = {
"555": "TOTO",
"556": "MAMA",
"557": "PAPA",
"1": "ZOE",
"2": "ABC"
};
function sortMap(map){
// Get the keys of map
var keys= Object.keys(map);
// Sort the keys by the value
keys.sort(function(a,b){
var aValue = map[a];
var bValue = map[b];
if(aValue < bValue) return -1;
if(aValue > bValue) return 1;
return 0;
});
// Rebuild the map from the sorted key.
var result = new Map;
for(var i = 0; i < keys.length; i++){
result.set(keys[i], map[keys[i]]);
}
return result;
}
var sortedMap = sortMap(myMap);
// Snipet display
sortedMap.forEach(function(value, key){
document.body.appendChild(document.createElement('div')).innerHTML = key+'=>'+value;
});