#340 enhancement
Martin Lie


Reported by Martin Lie | September 14th, 2008 @ 11:50 PM

Searching the tickets for "shuffle" didn't return anything, so I'll dare propose this method for Enumerable:

shuffle: function (iterable) { return $A(iterable).sortBy(Math.random); }

Comments and changes to this ticket

  • Juriy Zaytsev

    Juriy Zaytsev September 15th, 2008 @ 01:26 AM

    • State changed from “new” to “enhancement”

    I think it's 1) too trivial to implement, 2) is rarely needed:

    Array.prototype.shuffle = function(){
      return this.sortBy(Math.random);
    // vs

    Second version (that is based on your proposal) is literally few characters longer. Would it really make sense to add another method to Enumerable (and Array.prototype) for such rarely needed functionality?

  • Laurent Fortin

    Laurent Fortin September 15th, 2008 @ 02:47 AM


    I don't know if you got inspired by my little Prototype extension(Math.extend on scripteka.com), but thanks for the enhancement to Array#shuffle! I may apply a patch using your code suggestion.


  • Martin Lie

    Martin Lie September 15th, 2008 @ 12:57 PM

    Juriy: I was kinda expecting you'd say something like that. My counter-argument(s) would be: 1) even if it's trivial, it's very nice to have as a convenience method, and after all Prototype have quite a few of these "nice to have"-methods already (some of which are good reasons to love Prototype) and 2) even if the code itself is trivial, it's actually not easy to realize that it's as simple as this - search on "javascript array shuffle" or something like that and you'll find that most suggestions involve implementing some kind of sort-like algorithm on their own (instead of utilizing the built-in sort (or Prototype's sortBy)). If you still don't think these arguments are good enough, I won't argue any more. ;)

    Laurent: Nope, I haven't seen it. I'll check it out. :)

  • Laurent Fortin

    Laurent Fortin September 15th, 2008 @ 06:31 PM


    I have already suggested this kind of nice addon to the Prototype framework core(for instance, Date#getLastDayOfMonth), but because it was not a so common usage stuff, and because the core team want to keep the size of the prototype.js file under an acceptable limit, it was labeled as 'not for core' (and I agreed with the core team's decision.).

    Let's keep prototype.js a small file!

  • T.J. Crowder

    T.J. Crowder November 16th, 2009 @ 04:50 PM

    [responsible:none bulk edit command]

  • Tobie Langel

    Tobie Langel March 1st, 2010 @ 12:42 AM

    • Tag set to section:lang

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

The Prototype JavaScript library.

Shared Ticket Bins