JavaScript advanced file directory sorting code _javascript tips

Source: Internet
Author: User
Just finished, welcome everyone to help test
Complete test Code:
<!doctype html> <ptml> <pead> <meta charset= "gb2312"/> <title> advanced directory, file name ordering </title&gt ; <meta name= "description" content= "JavaScript advanced directory, file name sorting implementation"/><!--[if]8/26/2010--> <meta name= " Keywords "content=" javascript advanced directory, file name sorting implementation, Win7 sorting, sort "/> <style> #page {} </style> </pead> <b ody> <div id= "page" > <p> advanced directory, filename sorting </p> <div> Main implementation of the numbers in the directory by size of the order of the </div>: <br& Gt </div> </body> <script>//accepts a string; Returns the string with the regex metacharacters escaped. The returned string//can safely be used in any point within a regex to match the provided literal string. escaped//characters are [,], {,}, (,),-, *, +,?, ., \, ^, $, |, #, &LT;COMMA&GT;, 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) {//Last file, add Res[i]=item directly; }else {///escape regular special characters 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), a)-parseint (Getval (b). substr (k), 10); }); Res=res.concat (temp); temp=[];j=0; if (i!==arr.length-1) {i-=1;//minus 1 to perform a lookup so that the while end of Str}}} return res; var arr=["//", ",", "3", "...", "8", "2", "" "," ",", ",", "...", "1.jpg", "9.jpg", "234", "99", "1" 3.jpg "," 8.jpg "," Idkinbec "," Idkinbed3 "," Idkinbef "," Idkinbegh "," Idkin44.txt "," Idkinb1.txt "," Idkinbd.txt "," Idkinbe.txt "," Idkinbe3.txt "," Idkin (be12.txT ",//Regular special characters" Idkinbe5.txt "," Idkinbe34.txt "," Idkinbe25.txt "," Idkinb.txt "," Idkin23.txt "," Idkin1.txt "," Idkin5.tx T "," idk "]; var res=filesort (arr); for (Var i=0;i<res.length;i++) {document.write (res[i]+ "<br>"); } document.write ("The number of files is:" +arr.length+ "/" +res.length); arr=[{name: ' BB ', size: ', {name: ' A ', Size: ' 5 '}, {name: ' 2 ', Size: ' 9 '}, {name: ', ', Size: ' 3 '}] Res=filesort (arr,funct Ion (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> file number is:" +arr.length+ "/" +res.length); </script> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

The main sorting function source code is as follows (v1):
Copy Code code as follows:

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) {//The punctuation before the number begins
Res[i]=cur;
if (i===arr.length-1) return res;
Cur=arr[++i];
}
while (cur.charcodeat (0) <58) {///begins with a number
Temp[j++]=cur;
if (i===arr.length-1) break;
Cur=arr[++i];
}
Temp.sort (function (a,b) {//Sort begins with a number
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) {//last file added directly
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), ten)-parseint (B.substr (k), 10);
});
Res=res.concat (temp);
temp=[];j=0;
i-=1;//minus 1, then performs the lookup so that the while end of STR
}
}
}
return res;
}

Think later, in the previous version, the first two while is completely redundant, and after the removal, the direct support of the special characters after the order containing the numbers
V2 (note regexp not yet escape):
Copy Code code as follows:

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) {//last file added directly
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), ten)-parseint (B.substr (k), 10);
});
Res=res.concat (temp);
temp=[];j=0;
i-=1;//minus 1, then performs the lookup so that the while end of STR
}
}
}
return res;
}

In addition, the recent use of JQ findings:
1:hidden This selection selects all invisible elements, and if the elements of the clone () are not added to the DOM, they are invisible
2. Serialize () When using this method, you need to specify name for the form element in the form, otherwise this method returns an empty string
3 Jstree plug-in can not be used with earlier version of the Validate plug-in, because the earlier version of the Validate JQ delegate method, updated version of the good
The DatePicker plugin in the 4 UI cannot be bound to a text box with a. Hasdatepicker, you need to remove this class name before you can
5 Closest,live (delegate) is a really good way

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.