//一維數(shù)組的排序
// type 參數(shù)
// 0 字母順序(默認(rèn))
// 1 大小 比較適合數(shù)字?jǐn)?shù)組排序
// 2 拼音 適合中文數(shù)組
// 3 亂序 有些時(shí)候要故意打亂順序,呵呵
// 4 帶搜索 str 為要搜索的字符串 匹配的元素排在前面
function Array.prototype.SortBy(type,str)
{
switch (type)
{
case 0:this.sort(); break;
case 1:this.sort(function(a,b){ return a-b; }); break;
case 2:this.sort(function(a,b){ return a.localeCompare(b) }); break;
case 3:this.sort(function(){ return Math.random()>0.5?-1:1; }); break;
case 4:this.sort(function(a,b){ return a.indexOf(str)==-1?1:-1; }); break;
default:this.sort();
}
}
隨機(jī)數(shù)組
<script>
var ars=['金','木','水','火','土','仁','義','禮','志','賢'];
for(var i=ars.length-1;i>0;i--)
ars.push(ars.splice(Math.round(Math.random()*i), 1))
alert(ars)
</script>
<script>
//隨機(jī)重排數(shù)組,by kill,20:39 03-4-14
//產(chǎn)生隨機(jī)數(shù):
rnd.today=new Date();
rnd.seed=rnd.today.getTime();
function rnd()
{
rnd.seed = (rnd.seed*9301+49297) % 233280;
return rnd.seed/(233280.0);
}
function rand(number) {return Math.ceil(rnd()*number);}
//原數(shù)組
var arr = new Array("1","2","3","4","5","6","7","8","9","10");
out_put(arr);
document.write("<hr width=180 align=left><input type='button' onclick='location.reload()' value=' refresh '></input><br><br>");
//隨機(jī)重排數(shù)組后輸出:
for(var i=0;i<10;i++)
{
arr_rand(arr);
out_put(arr);
}
//數(shù)組輸出函數(shù)
function out_put(arr)
{
for(var i=0;i<arr.length;i++)
document.write(" "+arr[i]);
document.write("<br>");
}
//數(shù)組重組函數(shù),將每個(gè)元素與一隨機(jī)元素?fù)Q位:
function arr_rand(arr)
{
for(var i=0;i<arr.length;i++)
{
var tem,ranarr=rand(arr.length-1);
tem=arr[i];
arr[i]=arr[ranarr];
arr[ranarr]=tem;
}
}
</script>
<style>*{font-size:35px;font-weight:bolder;letter-spacing:10px;overflow:hidden;text-decoration:underline}</style>
<script>
var ars,str='';ars=['金','木','水','火','土','仁','義','禮','志','賢'];
for(j=0;j<10;j++){for(i=0;i<10;i++){str+=ars[Math.round(10*Math.random())];if(/(.).*\1/ig.test(str))str=str.substring(0,i--)}document.write(str+'
');}
</script>
<script>
ars=['金','木','水','火','土','仁','義','禮','志','賢']
for (var i=0; i<10; i++)
document.write(ars.sort(function(){return Math.random( )*new Date%3-1})+"<br />")
</script>