Simulation of Windows files by name based on JS _javascript tips

Source: Internet
Author: User

The

makes a record, mainly on the processing of numbers, and if the preceding characters are the same, the numbers are compared by values rather than by individual characters.

function Sortlikewin (v1, v2) {var a = V1.name; var b = v2.name; var reg =/[0-9]+/g; var lista = A.match (reg); var Listb
= B.match (reg); if (!lista | |!listb) {return a.localecompare (b);} for (var i = 0, Minlen = math.min (Lista.length, listb.length); ; Minlen; i++) {//numeric location ordinal var indexa = A.indexof (Lista[i]); var indexb = B.indexof (listb[i));//number preceding prefix var Prefixa = a.substring (
0, Indexa);
var prefixb = a.substring (0, INDEXB);
string var stra = lista[i] of numbers;
var strb = listb[i];
The value of the number var Numa = parseint (stra);
var numb = parseint (STRB); If the numbers are unequal or prefixed with different prefixes, they are compared directly to the IF (indexa!= indexb | | Prefixa!= PREFIXB) {return a.localecompare (b);} else {//number str ing equality if (stra = = STRB) {//If the last digit, compare the suffix of the number if (i = = minLen-1) {return a.substring (indexa). Localecompare (b.substring
(INDEXB)); 
//If not the last number, the loop jumps to the next number and removes the same part else {a = a.substring (Indexa + stra.length); b = b.substring (Indexa + stra.length); }//If the number string is not congruent, but the value is equal else if (numa = = numb) {//Direct comparison of the number prefix 0Number, more small return strb.lastindexof (numb + ')-stra.lastindexof (Numa + ');
else {//If number is not equal, direct comparison number size return Numa-numb}} }
}

Use method, Array.Sort (Sortlikewin);

The above mentioned is a small set of JS simulation based on the Windows file according to name sorting effect, hope to help everyone, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.