PDA

View Full Version : Random sort of a list?



gwmbox
03-20-2010, 10:25 AM
Hi guys

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


<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

vwphillips
03-20-2010, 10:58 AM
<!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>

gwmbox
03-20-2010, 11:05 AM
wow thanks for such a quick reply with a solution, please know it is greatly appreciated

Cheers