标签:style blog color os java io strong ar div
简单的js数组排序:
<span style="font-size:18px"><script LANGUAGE=javascript> var arr=[112,6,3,77,33,11,4]; arr.sort(function(a,b){return a-b}); alert(arr.toString());//3,4,6,11,33,77,112 </script></span>
二元数组排序:
<span style="font-size:18px"><script LANGUAGE=javascript> var aa=[]; aa[0]=["4","aa1"]; aa[1]=["3","aa2"]; aa[2]=["1","aa3"]; aa[3]=["2","aa4"]; aa.sort(function(a,b){return a[0]-b[0]}); alert(aa.toString()); //1,aa3,2,aa4,3,aa2,4,aa1 </script></span>
强化版数组排序:
<span style="font-size:18px"><script LANGUAGE=javascript> // str 一维数组的排序 // type 参数 // 0 字母顺序(默认) // 1 大小 比较适合数字数组排序 // 2 拼音 适合中文数组 // 3 乱序 有些时候要故意打乱顺序,呵呵 // 4 时间 var arr=[112,6,3,77,33,11,4]; function SortBy(type,str) { switch (type) { case 0:str.sort(); break; case 1:str.sort(function(a,b){ return a-b; }); break; case 2:str.sort(function(a,b){ return a.localeCompare(b) }); break; case 3:str.sort(function(){ return Math.random()>0.5?-1:1; }); break; case 4:str.sort(function(a,b){return parseInt(a[0].replace(/\-/g,""),10)-parseInt(b[0].replace(/\-/g,""),10)}); break; default:str.sort(); } return str; } alert(SortBy(1,arr)); //3,4,6,11,33,77,112 </script></span>
标签:style blog color os java io strong ar div
原文地址:http://www.cnblogs.com/likers/p/3937459.html