Borislav Hadzhiev
Wed Dec 08 2021·2 min read
Photo by Elias Ehmann
To sort a Set
in JavaScript:
Set
into an array, using the Array.from()
method.Array.sort()
method.Set
object./** * 👇️ SORT a NUMBERS Set */ 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); // 👉️ [100, 300, 700] const sortedNumbersSet = new Set(sortedNumbers); console.log(sortedNumbersSet); // 👉️ {100, 300, 700} /** * 👇️ SORT a STRINGS Set */ const stringsSet = new Set(['c', 'b', 'a']); const sortedStrings = Array.from(stringsSet).sort(); console.log(sortedStrings); // 👉️ ['a', 'b', 'c'] const sortedStringsSet = new Set(sortedStrings); console.log(sortedStringsSet); // 👉️ {'a', 'b', 'c'}
We used the
Array.from
method to create an array from the Set
object.
We then called the Array.sort method on the array.
Notice that when sorting numbers, we have to pass a function as a parameter to
the sort
method, whereas with strings, we don't.
The parameter we passed to the sort
method is a function that defines the sort
order.
Sets
that contain numbers, however it's exactly what we want when comparing strings.After we have sorted the array, we have to pass it to the
Set constructor
to convert it back to a Set
. We can iterate over Sets
in element insertion
order.
Array.from
method is the recommended approach when using TypeScript because the compiler often complains when using the spread operator (...) with iterators.Here are the same examples, however this time we use the
spread operator (...)
instead of Array.from
.
const numbersSet = new Set([300, 100, 700]); const sortedNumbers = [...numbersSet].sort((a, b) => a - b); console.log(sortedNumbers); // 👉️ [100, 300, 700] const sortedNumbersSet = new Set(sortedNumbers); console.log(sortedNumbersSet); // 👉️ {100, 300, 700} /** * 👇️ SORT a STRINGS Set */ const stringsSet = new Set(['c', 'b', 'a']); const sortedStrings = [...stringsSet].sort(); console.log(sortedStrings); // 👉️ ['c', 'b', 'a'] const sortedStringsSet = new Set(sortedStrings); console.log(sortedStringsSet); // 👉️ {'a', 'b', 'c'}
Set
into an array.