1 Reply Latest reply: Nov 11, 2014 3:04 AM by kashan RSS

    shuffle or randomize XML

    pamelerr Community Member

      I've searched this forum for a quick way to shuffle nodes in XML. I found this function on shuffling arrays (http://www.ultrashock.com/forum/viewthread/123580/) and I modified it for xml:

       

       

      function shuffleMe(xml:XML):XML
      {
        var len:uint = xml.children().length();
        for (var i:uint=0; i < len; i++)
        {
            var rand:uint = Math.floor(Math.random() * len);
            //swap current index with a random one
            var temp:* = xml.children()[i];
            xml.children()[i] = xml.children()[rand];
            xml.children()[rand] = temp;
        }

        return xml
      }

       

       

      Hope this helps someone...

        • 1. Re: shuffle or randomize XML
          kashan Community Member

          Thank you.

          Here is randomizing XMLList, if someone should be looking for that.

           

          myXMLList = XMLListShuffle(myXMLList);

           

          function XMLListShuffle(sourceList:XMLList):XMLList  {

          var randomized:XMLList = new XMLList();

          while(sourceList.length()) 

          {

            var r:int = Math.floor(Math.random() * sourceList.length());

            randomized += sourceList[r];

            delete sourceList[r];

          }

          return randomized;

          }