sort map by key in javascript code example
Example 1: array sort by key javascript
function sortByKey(array, key) {
return array.sort((a, b) => {
let x = a[key];
let y = b[key];
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
});
}
Example 2: javascript sort map by value
var mapAsc = new Map([...map.entries()].sort((a,b) => a[0] > b[0]));