How to sort a Set in JavaScript

Sort a Set in JavaScript

To sort a Set in JavaScript:

  1. Use the Array.from() method to convert the Set to an array.
  2. Sort the array using the Array.sort() method.
  3. Convert the sorted array back into a Set.

Sort a number set

Example –

const numbersSet = new Set([300, 100, 700]);

// sorts numbers in Ascending order
const sortedNumbers = Array.from(numbersSet).sort((a, b) => a - b);
console.log(sortedNumbers); 

const sortedNumbersSet = new Set(sortedNumbers);
console.log(sortedNumbersSet);
Sort a number set.
Sort a number set.

Here’s what the above code is doing:

  • We create a numbersSet instance of Set class.
  • We then use Array.from() method to convert numbersSet into an array and sort the array.
  • The result is then converted back into a Set instance using the Set constructor.

The Array.sort() method sorts the elements of an Array in place and returns the sorted Array.
The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

The Array.sort() method takes an optional compareFunction parameter.
The compareFunction is a function that defines the sort order.
If omitted, the Array.sort() method sorts the elements with the sort order based on the Unicode code point values of elements.

The compareFunction takes two parameters a and b which are the two elements being compared.
The function returns a negative number if a should come before b, a positive number if a should come after b, or 0 if they are equal.

The Array.sort() method sorts the elements in ascending order by default.

Sort a STRINGS set

Example –

const stringsSet = new Set(['c', 'b', 'a']);

const sortedStrings = Array.from(stringsSet).sort();
console.log(sortedStrings); 

const sortedStringsSet = new Set(sortedStrings);
console.log(sortedStringsSet);
Sort a strings set.
Sort a strings set.

Here’s what the above code is doing:

  1. Create a Set of strings.
  2. Convert the Set to an Array.
  3. Sort the Array.
  4. Convert the sorted Array back to a Set.

The Array.sort() method for numbers requires a function to be passed as a parameter, but the sort method for strings does not. The reason for this is that the sort method for strings sorts them alphabetically, whereas the sort method for numbers sorts them numerically.

Sort a Set in JavaScript using spread operator (…)

Sort a number set

Example –

const numbersSet = new Set([20, 30, 40]);

const sortedNumbers = [...numbersSet].sort((a, b) => a - b);
console.log(sortedNumbers); 

const sortedNumbersSet = new Set(sortedNumbers);
console.log(sortedNumbersSet); 
Sort a number set using spread operator.
Sort a number set using spread operator.

Here’s what the above code is doing:

  1. We create a Set with the numbers 20, 30, and 40.
  2. We use the spread operator to convert the Set to an array.
  3. We sort the array using the sort() method.
  4. We create a new Set from the sorted array.
  • The sort method
    • sorts the numbers in ascending order
    • the sort method returns an array
  • The spread operator
    • spreads the array into individual elements
    • the spread operator returns a new array
  • The Set constructor
    • creates a new Set from the array
    • the Set constructor returns a new Set

Sort a strings set

Example –

const stringsSet = new Set(['c', 'b', 'a']);

const sortedStrings = [...stringsSet].sort();
console.log(sortedStrings); 

const sortedStringsSet = new Set(sortedStrings);
console.log(sortedStringsSet);
Sort a strings set using spread operator.
Sort a strings set using spread operator.

Here’s what the above code is doing:

  1. First, we create a Set of strings.
  2. Then, we use the spread operator to convert the Set to an Array.
  3. Next, we sort the Array.
  4. Finally, we create a new Set from the sorted Array.

The result is a Set that’s sorted in ascending order.

You can also use the spread operator to convert a Set to an Array, and then use the Array.prototype.reverse() method to sort the Array in descending order.

The most common way to convert a set into an array is to use the spread operator (…).

The Array.from() method is the recommended approach to use when working with TypeScript, as the compiler will often throw errors when using the spread operator (…) with iterators.