Skip to content

Instantly share code, notes, and snippets.

@cggaurav
Forked from guilhermepontes/shuffle.js
Created December 31, 2018 14:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cggaurav/725c11c5a7d629e8a2a1bde60ba1730e to your computer and use it in GitHub Desktop.
Save cggaurav/725c11c5a7d629e8a2a1bde60ba1730e to your computer and use it in GitHub Desktop.
Shuffle Array - JavaScript ES2015, ES6
// original gist
const shuffleArray = arr => arr.sort(() => Math.random() - 0.5);
// fully random by @BetonMAN
const shuffleArray = arr => arr
.map(a => [Math.random(), a])
.sort((a, b) => a[0] - b[0])
.map(a => a[1]);
shuffleArray([1, 2, 3]) //[3, 1, 2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment