作为笔记使用,仅个人理解,如有错误,还望指出
由于js对map的存储会按照key自动排序,如果需要按map的值进行排序,需要另外处理,此处我需要按照存储的中文值的音序进行排序。
function sortMap(map){
let tempMap = new Map();
for (let key in map){
tempMap.set("id_" + key, map[key]);
}
let arr = Array.from(tempMap);
arr.sort(function (a, b) {
return a[1].localeCompare(b[1]);
});
let object = arr.reduce((obj, [key, value]) => Object.assign(obj, { [key]: value} ), {});
return object;
}