How to sort a collection in JavaScript ?
Last Updated :
16 Jan, 2022
A javascript collection is much like a container. It's just an item that combines several elements into a single unit. Aggregate information is stored, accessed, modified, and communicated via collections.
With the help of constructors, we create collections in javascript. In earlier versions of javascript constructors are expressed as functions and utilized in the same way. under collections come arrays, sets, and maps. Let's learn how to sort them in this article.
We use the JavaScript sort() method to sort collections. This method sorts the array in place.
Syntax:
Array.prototype.sort()
SORTING AN ARRAY
Example 1: Sorting an array when the array has numbers as elements:
HTML
<!DOCTYPE html>
<html>
<head>
<title>Javascript sorting collections</title>
</head>
<body>
<script>
// Sorting an array in ascending order
let array = [10, 2, 5, 12, 7];
array = array.sort(function (a, b) {
return a - b;
});
console.log("sorted array : " + array);
</script>
</body>
</html>
Output:
Example 2: Sorting an array when array consists of strings:
HTML
<!DOCTYPE html>
<html>
<head>
<title>Javascript sorting collections</title>
</head>
<body>
<script>
// Sorting an array of strings
let array = ["c", "b", "a"];
array = array.sort();
console.log("sorted array : " + array);
</script>
</body>
</html>
Output:

SORTING A MAP
Sorting a map when:
- Maps have pairs of keys and values.
- Maps work like dictionaries.
- We've to convert the Map to Array to sort.
Example 1: Sorting according to values:
HTML
<!DOCTYPE html>
<html>
<head>
<title>Javascript sorting collections</title>
</head>
<body>
<script>
// Creating a map and sorting it according to values.
let newMap = new Map();
newMap.set("a", 50);
newMap.set("c", 40);
newMap.set("b", 30);
newMap.set("d", 10);
newMap = Array.from(newMap).sort((a, b) => a[1] - b[1]);
console.log(newMap);
</script>
</body>
</html>
Output:
Example 2: Sorting according to keys:
HTML
<!DOCTYPE html>
<html>
<head>
<title>Javascript sorting collections</title>
</head>
<body>
<script>
// Sorting map by keys
let newMap = new Map();
newMap.set("a", 50);
newMap.set("c", 40);
newMap.set("b", 30);
newMap.set("d", 10);
newMap = Array.from(newMap.entries()).sort();
console.log(newMap);
</script>
</body>
</html>
Output:

SORTING A SET
Sorting a set when:
- We create a new set using the new keyword.
- The set contains duplicate values which are removed after the code is run.
- We need to convert the set created to Array in order to sort it.
Example 1: Sorting a set that contains numbers:
HTML
<!DOCTYPE html>
<html>
<head>
<title>Javascript sorting collections</title>
</head>
<body>
<script>
// Converting a set into an array and sorting it.
new_set = Array.from(new Set([9, 9, 2, 4, 5, 11]))
.sort(function (a, b) {
return a - b;
});
console.log(new_set);
</script>
</body>
</html>
Output:
Example 2: Sorting a set of strings:
HTML
<!DOCTYPE html>
<html>
<head>
<title>Javascript sorting collections</title>
</head>
<body>
<script>
new_set = Array.from(new Set(["rachel", "sam", "daniel"]))
.sort();
console.log(new_set);
</script>
</body>
</html>
Output:
Similar Reads
How to sort a set in JavaScript ? Sorting a Set based on the values of its elements is a common need in JavaScript. While the built-in sort() function can't be directly applied to a Set, there's a workaround. Let's explore how we can achieve this: Instead of directly sorting the Set, we'll: Convert the Set into an array to make it s
3 min read
How to Sort a Dictionary by Value in JavaScript ? In JavaScript, dictionaries do not have a built-in method to sort them by their values. However, there are several approaches to achieve this functionality. Below are the possible Approaches: Table of Content Using Array.sort() with Object.entries()Using a custom sort functionUsing Array.sort() with
2 min read
How to Sort a Map in JavaScript? Sorting a Map in JavaScript involves ordering its key-value pairs based on the keys or values. Since Maps maintain the insertion order, you can't directly sort them like arrays. Instead, you'll need to convert the Map into an array, sort it, and then convert it back into a Map.Below are the approach
3 min read
How to sort a collection ? In this article, we will learn how to sort the items in a collection. The sort() method is used for this purpose. A collection takes the input of an array containing numbers, strings, or objects. Syntax: collect(array).sort()Parameters: It takes an array as a parameter that is converted into a coll
3 min read
Sort an Array in JavaScript This article will show you how to sort a JS array.1. Using array.sort() Method To sort an array in JavaScript, we can use array.sort() method. This method sorts the elements alphabetically in ascending order.JavaScriptlet arr = ["JS", "HTML", "CSS"]; arr.sort() console.log(arr);Output[ 'CSS', 'HTML'
3 min read