0

I purpose the most lightweight way (from performance point of view) to shuffle an array is

document.write([1, 2, 3, 4].sort(function() {
    return 0.5 - Math.random();
}));

Isn't it?

kissarat
  • 19
  • 1
  • 5
  • 2
    Nope, you have nxn complexity due to sorting, while these solutions iterate only once http://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array ... maybe yours can be the shortest one :) – gurvinder372 Apr 04 '16 at 12:27
  • That is also a non-consistent comparator. I wonder if depending on the sort algorithm used that would result in problems. For example, in Java, you can get run-time-exceptions with that. – Thilo Apr 04 '16 at 12:32
  • Duplicate of this answer: http://stackoverflow.com/a/18650169/14955 (with a couple of comments on why this method is quite bad). – Thilo Apr 04 '16 at 12:34

0 Answers0