高级的文件目录排序
最近工作比较忙,好久没写文章了,这几天在做一个文件管理的模块,里面有排序的功能,产品经理看了说希望能做出更加智能的文件排序功能,就像是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)真是好用的方法
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器