Results 1 to 3 of 3

Thread: Random sort of a list?

  1. #1
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Default Random sort of a list?

    Hi guys

    I wonder if you can help me, if I have a list of items

    Code:
    <li>item</li>
    <li>item2</li>
    and so on....
    and there can be between 15 and 100 of them listed, is it possible for that list to be randomly sorted on each page load?

    Thanks
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

  2. #2
    Join Date
    Dec 2008
    Location
    Portsmouth, UK
    Posts
    1,891
    Thanks
    2
    Thanked 441 Times in 435 Posts

    Default

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <ul id="tst">
      <li>tom</li>
      <li>****</li>
      <li>harry</li>
    </ul>
    
    <script type="text/javascript">
    /*<![CDATA[*/
    
    function RandomList(id){
     var ul=document.getElementById(id);
     var lis=ul.getElementsByTagName('*')
     for (var ary=[],z0=0;z0<lis.length;z0++){
      if (lis[z0].parentNode==ul){
       ary.push(lis[z0]);
      }
     }
     ary.shuffle();
     for (var z1=0;z1<ary.length;z1++){
      ul.appendChild(ary[z1]);
     }
    }
    
    Array.prototype.shuffle=function(){
     for (var r,t,z0=0;z0<this.length;z0++){
      r=Math.floor(Math.random()*this.length);
      t=this[z0];
      this[z0]=this[r];
      this[r]=t;
     }
    }
    
    RandomList('tst')
    /*]]>*/
    </script>
    
    </body>
    
    </html>
    Vic
    God Loves You and will never love you less.
    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

  3. #3
    Join Date
    Mar 2005
    Location
    Western Australia
    Posts
    148
    Thanks
    24
    Thanked 4 Times in 4 Posts

    Default

    wow thanks for such a quick reply with a solution, please know it is greatly appreciated

    Cheers
    1st rule of web development - use Firefox and Firebug
    2nd rule - see the first rule
    --
    I like Smilies

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •