How to sort a set in JavaScript ?
A Set in JavaScript is a special kind of object that stores distinct elements. The set can store both primitive as well as objects. When iterating over the set object, it returns the elements in the same order as inserted.
In this article, we will see how to sort the Set according to the value of the elements. One way of achieving the target is to use the in-built sort() function. But, the sort() function can’t be directly implemented on set, rather:
- We will generate an array container using the elements stored in the set.
- Then, we will execute the sort() function on the array.
- We shall clear the entire set and store the elements stored in the newly sorted array.
As the set returns elements in the same order as inserted, the whole set will now be sorted.
One thing to note is that here in this article we will sort the set in ascending order, but the sorting order can be adjusted according to the need by implementing sorting logic. Now we shall look at different approaches to generating arrays to achieve our purpose.
Approach 1. Manually iterating over the set: In this approach, we will iterate over the set manually and insert elements of the set into a new array that is about to get sorted according to our needs.
Syntax:
# myset is the set under consideration for this article
let myarr = [];
for (let element of myset) {
// Set elements pushed into the array
myarr.push(element);
}
# myArray consists of the elements of the set
myArray.sort()Example:
JavaScript
<script> // Initialize a Set object // using Set() constructor let myset = new Set() // Insert new elements in the // set using add() method myset.add(3); myset.add(2); myset.add(9); myset.add(6); // Print the values stored in set console.log(myset); // Create a new array to store set elements let myarr = []; for (let element of myset) { // Set elements pushed into the array myarr.push(element); } // Print the values stored in Array console.log(myarr); // Sort the array (default it will sort // elements in ascending order) myarr.sort() // Clear the entire set using clear() method myset.clear() for (let element of myarr) { // Array elements pushed into the set myset.add(element); } // Print the values stored in set console.log(myset);</script> |
Output:
Set(4) { 3, 2, 9, 6 }
[ 3, 2, 9, 6 ]
Set(4) { 2, 3, 6, 9 }Approach 2. Using Array.from() method: One way of generating the array is to use the in-built Array.from() method which will create an array out of the elements of the set. To know more about the method, follow this link.
Syntax:
# myset is the set under consideration for this article myArray = Array.from(myset) myArray.sort()
Example:
JavaScript
<script> // Initialize a Set object // using Set() constructor let myset = new Set() // Insert new elements in the // set using add() method myset.add(3); myset.add(2); myset.add(9); myset.add(6); // Print the values stored in set console.log(myset); // Create a new array to store set elements let myarr = []; for (let element of myset) { // Set elements pushed into the array myarr.push(element); } // Print the values stored in Array console.log(myarr); // Sort the array (default it will sort // elements in ascending order) myarr.sort() // Clear the entire set using clear() method myset.clear() for (let element of myarr) { // Array elements pushed into the set myset.add(element); } // Print the values stored in set console.log(myset);</script> |
Output:
Set(4) { 3, 2, 9, 6 }
[ 3, 2, 9, 6 ]
Set(4) { 2, 3, 6, 9 }




Please Login to comment...