How to Sort a Es6 Map Object

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)

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; }

Is it possible to sort a ES6 map object for a property of its value object?

You could sort the entries of Map based on Segment by using localeCompare. Then create a new Map from the sorted entries

var map=new Map();map.set("8014",{"Access to support":null,Capacity:1,"Comfort Level":null,"Control Level":1,"Growth Mindset":1.5405405405405403,Horizon:null,Motivation:null,"Open-Mindedness":null,Proactivity:null,Resiliency:null,Segment:"Level value 1"});map.set("8015",{"Access to support":null,Capacity:1,"Comfort Level":null,"Control Level":1,"Growth Mindset":1.5405405405405403,Horizon:null,Motivation:null,"Open-Mindedness":null,Proactivity:null,Resiliency:null,Segment:"Level value 2 or 5"});map.set("8016",{"Access to support":null,Capacity:1,"Comfort Level":null,"Control Level":1,"Growth Mindset":1.5405405405405403,Horizon:null,Motivation:null,"Open-Mindedness":null,Proactivity:null,Resiliency:null,Segment:"Level value 3 or 5"});
const sortedMap = new Map( Array.from(map) .sort((a, b) => a[1].Segment.localeCompare(b[1].Segment)) );
console.log([...sortedMap.entries()])

How to sort a Map in ES6

sort() function, when you don't supply a compareFunction as an argument, does not really work the way you instinctively expect it to work. See the following quote from relevant MDN page:

If compareFunction is not supplied, all non-undefined array elements
are sorted by converting them to strings and comparing strings in
UTF-16 code units order. For example, "banana" comes before "cherry".
In a numeric sort, 9 comes before 80, but because numbers are
converted to strings, "80" comes before "9" in the Unicode order.
All
undefined elements are sorted to the end of the array.

The numeric sort bit in the quote explains why you're getting two different sorts with strings and numbers (with "0900" and 900). To overcome this, simply provide a function to the sort to the comparisons the way you want it, like so:

let ma = new Map([...m.entries()].sort((a, z) => a[0] - z[0]);

You can look into the details of how these compareFunctions work in the same MDN page: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort

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 a map by a specific field in es6 / React?

Assuming id is a number you can do products.sort(({id: previousID}, {id: currentID}) => previousID - currentID)

Like so:

JavaScript Code:

{products
.sort(({ id: previousID }, { id: currentID }) => previousID - currentID)
.map(({ id, headline }) => (
<Container key={id}>
<Row>
<Col>
<p>id={id}</p>
<p>headline={headline}</p>
</Col>
</Row>
</Container>
))
}


Related Topics



Leave a reply



Submit