How to Sort a Map by Value in JavaScript

How to sort a map by value in JavaScript?

Yo could take a different approach and change Symbol.iterator of Map.prototype[@@iterator]() for a custom sorted result.

var map = new Map();
map.set("orange", 10);map.set("apple", 5);map.set("banana", 20);map.set("cherry", 13);
map[Symbol.iterator] = function* () { yield* [...this.entries()].sort((a, b) => a[1] - b[1]);}
for (let [key, value] of map) { // get data sorted console.log(key + ' ' + value);}
console.log([...map]); // sorted orderconsole.log([...map.entries()]); // original insertation order
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sort a map by value in javascript and keep one field index to top

You could get the Map#entries of the map and sort the array.

To move a certain value to top (or keep this value at top), you need a return value for Array#sort which reflects the wanted order. In this case a is taken fom the conditions and if falsy, like zero, the second part after the logical OR || is taken.

let map = new Map();

map.set('0', "Select");
map.set('1', "foo");
map.set('2', "bar");
map.set('3', "baz");

result = Array.from(map).sort(([, a], [, b]) =>
(b === 'Select') - (a === 'Select') || a.localeCompare(b)
);

console.log(result);

Map and sort data by date

let data = [
{
"itemOne":111,
"itemTwo":1,
"itemThree":"2022-02-01T00:00:00.000Z"
},
{
"itemOne":222,
"itemTwo":2,
"itemThree":"2022-01-01T00:00:00.000Z"
},
{
"itemOne":333,
"itemTwo":3,
"itemThree":"2021-12-01T00:00:00.000Z"
},
]
data.sort((a,b)=>{
return new Date(b.itemThree) - new Date(a.itemThree);
})

This sorts your data array according to date.
Hope this is helpful

How to sort Map array of arrays based on values in Javascript

You can do this with map and Object.entries/Object.fromEntries

const input = new Map([
['Adopted', 105],
['Pending', 176],
['Missing', 3],
['InterestedParties', 45],
['WaitingForPickup', 15],
]);

const metricGroups = {
unAdopted: [
"Pending",
"InterestedParties",
],
adopted: [
"Adopted",
],
unknown: [
"Missing",
"WaitingForPickup"
],
}

const result = Object.fromEntries(Object.entries(metricGroups).map(([key, values]) => {
return [
key,
values.map(value => ([value, input.get(value)]))
]
}));

console.log(result);

Is it possible to sort a ES6 map object?

According MDN documentation:

A Map object iterates its elements in insertion order.

You could do it this way:

var map = new Map();
map.set('2-1', "foo");
map.set('0-1', "bar");
map.set('3-1', "baz");

var mapAsc = new Map([...map.entries()].sort());

console.log(mapAsc)

Sort Hashmap by Key Alphabetically if Values are Equal

You can use Array#sort and define a custom compare function to do this.

MDN Web Docs:

function compare(a, b) {
if (a is less than b by some ordering criterion) {
return -1;
}
if (a is greater than b by the ordering criterion) {
return 1;
}
// a must be equal to b
return 0;
}

In this case, we can first compare the values of a and b, then compare their keys if the values are equal.

Full code:

let map = new Map([ ["this", 1], ["is", 2], ["b", 2], ["a", 2], ["c", 2], ["test", 3], ["that", 1], ["not", 1], ]);

const mapSort1 = Array.from(map.entries()).sort(
([aName, aNumber], [bName, bNumber]) => {
if (aNumber === bNumber) {
if (aName < bName) return -1;
return 1;
} else if (aNumber > bNumber) return -1;
return 1;
}
);

console.log(mapSort1);

sort Map by value in javascript

You cannot sort a Map directly, but you can convert it to an array, sort it and create a new Map:

m = new Map()
m.set({x:1}, [1,2,3]);m.set({x:2}, [1,2,3,4]);m.set({x:3}, [1,2]);m.set({x:4}, [1,2,3,4,5]);m.set({x:5}, [1]);
// can also use Array.from polyfill herea = [];for(var x of m) a.push(x);
a.sort(function(x, y) { return x[1].length - y[1].length;}); sorted = new Map(a);for(var x of sorted) document.write(JSON.stringify(x) + "<br>");


Related Topics



Leave a reply



Submit