Created
October 12, 2018 23:33
-
-
Save tjjfvi/15a711258398c7330fbcf5c30c0c670f to your computer and use it in GitHub Desktop.
Array.prototype.shuffle: Shuffles array using custom Array.createRandomSortFunction()
This file contains 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
Array.createRandomSortFunction = () => { | |
let map = new Map([]); | |
let lookup = v => { | |
if(map.get(v)) return map.get(v); | |
let o = Math.random(); | |
map.set(v, o); | |
return o; | |
} | |
return (a, b) => lookup(a) - lookup(b); | |
}; | |
Array.prototype.shuffle = function(){ | |
return this.sort(Array.createRandomSortFunction()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment