Javascript
JS Set
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:

Steps to Sort a Set

Instead of directly sorting the Set, we’ll:

  1. Convert the Set into an array to make it sortable.
  2. Utilize the sort() function on the array to arrange its elements based on their values.
  3. Optionally, convert the sorted array back into a Set if needed.

By following these steps, we can effectively sort the Set according to the values of its elements.

Approaches to Sort a Set

Approach 1: By Iterating Over the Set

In this approach, we will iterate over the Set manually and insert its elements into a new array, which will then be sorted.

Syntax

// myset is the Set under consideration
let myarr = [];
for (let element of myset) {
    // Push Set elements into the array
    myarr.push(element);
}
// Sort the array
myarr.sort();

Example

// Initialize a Set object
let myset = new Set();
 
// Insert new elements into the Set
myset.add(3);
myset.add(2);
myset.add(9);
myset.add(6);
 
// Print original Set
console.log(myset);
 
// Convert Set to an array and sort
let myarr = [];
for (let element of myset) {
    myarr.push(element);
}
console.log(myarr);
myarr.sort();
 
// Clear and reassign sorted elements back to Set
myset.clear();
for (let element of myarr) {
    myset.add(element);
}
console.log(myset);

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 built-in Array.from() method, which creates an array from the elements of the Set.

Syntax

// Convert Set to an array
myArray = Array.from(myset);
myArray.sort();

Example

// Initialize a Set object
let myset = new Set();
 
// Insert new elements into the Set
myset.add(3);
myset.add(2);
myset.add(9);
myset.add(6);
 
// Print original Set
console.log(myset);
 
// Convert Set to an array and sort
let myArray = Array.from(myset);
myArray.sort();
console.log(myArray);
 
// Convert sorted array back to Set
myset.clear();
myset = new Set(myArray);
console.log(myset);

Output

Set(4) { 3, 2, 9, 6 }
[ 2, 3, 6, 9 ]
Set(4) { 2, 3, 6, 9 }

Approach 3: Using Spread Operator and sort() Method

In this method, we convert the Set into an array using the spread syntax (...), then use the sort() function to sort it.

Example

// Initialize a Set object
let myset = new Set();
 
// Insert new elements into the Set
myset.add(3);
myset.add(2);
myset.add(9);
myset.add(6);
 
// Print original Set
console.log(myset);
 
// Convert Set to sorted array and back to Set
const sortedArray = [...myset].sort();
const newSet = new Set(sortedArray);
console.log(newSet);

Output

Set(4) { 3, 2, 9, 6 }
Set(4) { 2, 3, 6, 9 }

Each of these methods provides a way to sort a Set in JavaScript by first converting it into an array, sorting it, and optionally converting it back into a Set.