高级的文件目录排序
最近工作比较忙,好久没写文章了,这几天在做一个文件管理的模块,里面有排序的功能,产品经理看了说希望能做出更加智能的文件排序功能,就像是win7的名称排序一样,主要就是文件名中的数字会按大小排序,而不是直接按ascii码 ,这两天晚上没事,就先写了这个排序方法,下个版本中就可以用上了
双击打开测试页面 ,刚写完,欢迎大家帮忙测试
v3(2010-8-29)修复之前的bug,并可对属性拓展排序
代码
//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"
];
/*
for(var i=0;i<5;i++){
arr=arr.concat(arr);
}
var d=new Date();*/
var res=fileSort(arr);
//document.write('timeout : '+(new Date()-d)+'<br/>');
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);
// 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"
];
/*
for(var i=0;i<5;i++){
arr=arr.concat(arr);
}
var d=new Date();*/
var res=fileSort(arr);
//document.write('timeout : '+(new Date()-d)+'<br/>');
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);
主要的排序功能源码如下(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;
}
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;
}
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,否则这个方法返回空字符串
3 jstree插件不能与早版本的validate插件同时使用,因为早版本的validate重新了jq的delegate方法,更新版本就好了
4 ui中DatePicker插件不能与有.hasDatepicker的文本框绑定 ,需要先去掉此类名,才行
5 closest,live(delegate)真是好用的方法