Skip to content

Instantly share code, notes, and snippets.

@DoctorDerek
Created November 1, 2021 01:26
Show Gist options
  • Save DoctorDerek/e98e591e15b5f2777f56e1e260865368 to your computer and use it in GitHub Desktop.
Save DoctorDerek/e98e591e15b5f2777f56e1e260865368 to your computer and use it in GitHub Desktop.
How To Sort a Set in JavaScript ES6 by Dr. Derek Austin 🥳 https://medium.com/p/51b53f6ef71a
const anArray = [5,3,3,3,4,2,1]
const aSet = new Set(anArray)
console.log(aSet) // Set(5) {5,3,4,2,1}
const uniqueArray = Array.from(aSet)
console.log(uniqueArray) // [5,3,4,2,1]
uniqueArray.sort((a,b)=>a-b)
// .sort((a,b)=>a-b)) to sort numbers
// just .sort() sorts alphabetically
console.log(uniqueArray) // [1,2,3,4,5]
const sortedSet = new Set(uniqueArray)
console.log(sortedSet) // Set(5) {1,2,3,4,5}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment