Created
November 1, 2021 01:26
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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