Skip to content

Instantly share code, notes, and snippets.

@tjjfvi
Created October 12, 2018 23:33
Show Gist options
  • Save tjjfvi/15a711258398c7330fbcf5c30c0c670f to your computer and use it in GitHub Desktop.
Save tjjfvi/15a711258398c7330fbcf5c30c0c670f to your computer and use it in GitHub Desktop.
Array.prototype.shuffle: Shuffles array using custom Array.createRandomSortFunction()
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