Fotiman - 2:38 pm on Apr 26, 2012 (gmt 0)
That's a neat trick! :) However, it's slightly less efficient, only because it may require iterating through an array several times as it moves elements up and down in the array. This can be demonstrated with this:
var c = 0,
cards = [1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10].sort(
alert('Number of calls to Math.random = ' + c);
For me, this is averaging around 54 calls to Math.random() with that array.
So it's neat and compact, but slightly less efficient. I also think it's less obvious what it's doing, so adds complexity, even though it's a smaller chunk of code. Still, great to see another way to skin the cat. :)