. Advertisement .
..3..
. Advertisement .
..4..
In JavaScript, Map is a particular type of object that is used for storing pairs of elements [key, value]. Either primitive or objects can be stored by the map. When the object of the map is iterated over, the returning value is a pair of [key, value] with the order same as inserted. In this blog, we will figure out how to sort a Map by keys in JavaScript.
To sort a Map by keys in JavaScript, you can use the available in-built function in Javascript called sort() function, which is created on the multi-dimensional array with support from the object’s array destructing created by the method Map.entries().
Syntax:
[...map1.entries()]
# this will generate a multi-dimensional array
# out of the key-value pairs stored in the map
We use the spread syntax (…) for getting an array in the entries of the Map which can be sorted by utilizing the sort method.
The object of the Map can remember the order of inserting the keys. This is the reason why the constructor Map() is used for creating the new Map ordered in a correct way. The spread syntax (…) helps us to having an array including the entries of the Map.
Syntax:
new Map()
new Map(iterable)
Example:
<script>
// map1 contains
// 1 => 10
// 2 => 20
// 3 => 30
// 4 => 40
var map1 = new Map([
[1 , 10], [2 , 20] ,
[3, 30],[4, 40]
]);
console.log("Map1: ");
console.log(map1);
</script>
Output:
Map1:
Map(4) { 1 => 10, 2 => 20, 3 => 30, 4 => 40 }
Note: This provides us an array including arrays of pairs [key, value], which can be passed directly to the constructor of the Map with purpose of creating sorted keys for the new Map.
Below is the completed snippet of code:
<script>
// Initializing and inserting values into Map
let map1 = new Map([
[4, 2],
[2, 3],
]);
// Inserting new element into map using set() method
map1.set(3, 10);
document.write("Our map :", "</br>");
document.write(map1, "</br>");
// Adding the sorting logic
map1 = new Map([...map1.entries()].sort());
// Separately printing only keys
for (let [key, value] of map1) {
document.write(key, " ");
}
</script>
Output:
Our map :
Map(3) { 4 => 2, 2 => 3, 3 => 10 }
2 3 4
Note: When the method sort() is called on the key-value pairs arrays, it just simply refers to the operation of sorting how the string of the key-value pairs is converted.
This method can also be used for sorting the keys of the maps in descending order. All you need to do is additionally perform the method reverse() after sorting.
<script>
let map1 = new Map([
[4, 2],
[2, 3],
]);
// Inserting new element into map using set() method
map1.set(3, 10);
document.write("Our map :", "</br>");
document.write(map1, "</br>");
// Adding the custom sorting logic to sort
// by decreasing values of keys
map1 = new Map([...map1.entries()].sort((a, b) => b[0] - a[0]));
// Separately printing only keys
for (let [key, value] of map1) {
document.write(key, " ");
}
</script>
Output:
Our map :
Map(3) { 4 => 2, 2 => 3, 3 => 10 }
4 3 2
In case the keys of the Map are numbers, a function must be passed to the method sort(). The method will take the parameter as the defining function for the order of sorting. 2 parameters will be get called with the function. You are introduced and demonstrated about how to use the method sort() to sort a Map by keys in JavaScript with specific code snippets. Hopefully, this would give you a helping hand in dealing with this functionality.
Leave a comment