剛寫完,歡迎大家幫忙測試
完整的測試代碼:
<!doctype HTML> <html> <head> <meta charset="gb2312" /> <title>進階目錄,檔案名稱排序</title> <meta name="description" content="Javascript進階目錄,檔案名稱排序實現"/><!--[if]8/26/2010--> <meta name="Keywords" content="Javascript進階目錄,檔案名稱排序實現,win7排序,sort"/> <style> #page{} </style> </head> <body> <div id="page" > <h4>進階目錄,檔案名稱排序</h4> <div>主要實現了目錄中的數字按大小排序</div> 排序結果:<br> </div> </body> <script> //accepts a string; returns the string with regex metacharacters escaped. the returned string // can safely be used at any point within a regex to match the provided literal string. escaped // characters are [, ], {, }, (, ), -, *, +, ?, ., \, ^, $, |, #, <comma>, and whitespace RegExp.escape = function (str) { return str.replace(/[-[\]{}()*+?.\\^$|,#\s]/g, "\\$&"); }; var fileSort=function(arr,getVal){ var res=[],temp=[],i=0,j=0,k,reg,item,cur; getVal=getVal || function(a){return a}; arr.sort(function(a,b){ return getVal(a).localeCompare(getVal(b)); }); for(;i<arr.length;i++){ cur=getVal(arr[i]); item=arr[i]; if((k=cur.search(/\d+/))===-1 || i===arr.length-1){//最後一個檔案時直接添加 res[i]=item; }else { //轉義正則特殊字元 reg=new RegExp("^"+RegExp.escape(cur.substr(0,k))+"\\d+"); while(reg.test(cur)){//more efficiency than search temp[j++]=item; if(i===arr.length-1)break; cur=getVal(arr[++i]); item=arr[i]; } if(temp.length>0){ temp.sort(function(a,b){ return parseInt(getVal(a).substr(k),10)-parseInt(getVal(b).substr(k),10); }); res=res.concat(temp); temp=[];j=0; if(i!==arr.length-1){ i-=1;//減1,再執行尋找使while結束的str } } } } return res; } var arr=[ "//", ",", "\"3", "...", "8", "2", "15", "\"21", ",", "...", "1.jpg", "9.jpg", "234", "99", "13.jpg", "8.jpg", "idkinbec", "idkinbed3", "idkinbef", "idkinbegh", "idkin44.txt", "idkinb1.txt", "idkinbd.txt", "idkinbe.txt", "idkinbe3.txt", "idkin(be12.txt",//正則特殊字元 "idkinbe5.txt", "idkinbe34.txt", "idkinbe25.txt", "idkinb.txt", "idkin23.txt", "idkin1.txt", "idkin5.txt", "idk" ]; var res=fileSort(arr); for(var i=0;i<res.length;i++){ document.write(res[i]+"<br>"); } document.write("檔案數為:"+arr.length+"/"+res.length); arr=[ {name:'bb',size:'12'}, {name:'a',size:'5'}, {name:'2',size:'9'}, {name:'12',size:'3'} ] res=fileSort(arr,function(item){ return item.size; }); for(i=0;i<res.length;i++){ document.write("<br>name:"+res[i].name+",size:"+res[i].size); } document.write("<br>檔案數為:"+arr.length+"/"+res.length); </script> </html>
[Ctrl+A 全選 注:如需引入外部Js需重新整理才能執行]
主要的排序功能源碼如下(v1):
複製代碼 代碼如下:
var fileSort=function(arr){
arr.sort();
var res=[],temp=[],i=0,j=0,k,reg,sc=true,cur=arr[0];
while(cur.charCodeAt(0)<48){//數字之前的標點等開頭的
res[i]=cur;
if(i===arr.length-1)return res;
cur=arr[++i];
}
while(cur.charCodeAt(0)<58){//以數字開頭的
temp[j++]=cur;
if(i===arr.length-1)break;
cur=arr[++i];
}
temp.sort(function(a,b){//排序以數字開頭的
return parseInt(a,10)-parseInt(b,10);
});
res=res.concat(temp);
temp=[];j=0;
for(;i<arr.length;i++){
cur=arr[i];
if((k=cur.search(/\d+/))===-1 || i===arr.length-1){//最後一個檔案時直接添加
res[i]=cur;
}else {
reg=new RegExp("^"+cur.substr(0,k)+"\\d+");
while(cur.search(reg)!==-1){
temp[j++]=cur;
if(i===arr.length-1)break;
cur=arr[++i];
}
if(temp.length>0){
temp.sort(function(a,b){
return parseInt(a.substr(k),10)-parseInt(b.substr(k),10);
});
res=res.concat(temp);
temp=[];j=0;
i-=1;//減1,再執行尋找使while結束的str
}
}
}
return res;
}
後來想想,上面的版本中,前面 兩個while完全是多餘 的,而且去掉之後 ,直接就支援了特殊字元後面中包含數位排序
v2(注意RegExp尚未escape):
複製代碼 代碼如下:
var fileSort=function(arr){
arr.sort();
var res=[],temp=[],i=0,j=0,k,reg,cur;
for(;i<arr.length;i++){
cur=arr[i];
if((k=cur.search(/\d+/))===-1 || i===arr.length-1){//最後一個檔案時直接添加
res[i]=cur;
}else {
reg=new RegExp("^"+cur.substr(0,k)+"\\d+");
while(cur.search(reg)!==-1){
temp[j++]=cur;
if(i===arr.length-1)break;
cur=arr[++i];
}
if(temp.length>0){
temp.sort(function(a,b){
return parseInt(a.substr(k),10)-parseInt(b.substr(k),10);
});
res=res.concat(temp);
temp=[];j=0;
i-=1;//減1,再執行尋找使while結束的str
}
}
}
return res;
}
另外,最近用jq的發現:
1 :hidden 這個選擇會選擇所有隱藏元素,如果是clone()的元素,還沒添加到dom時,都是屬於不可見
2 .serialize() 使用這個方法時,需要為form內的表單元素指定name,否則這個方法返回Null 字元串
3 jstree外掛程式不能與早版本的validate外掛程式同時使用,因為早版本的validate重新了jq的delegate方法,更新版本就好了
4 ui中DatePicker外掛程式不能與有.hasDatepicker的文字框綁定 ,需要先去掉此類名,才行
5 closest,live(delegate)真是好用的方法