Make a record mainly to deal with numbers. If the preceding characters of the numbers are the same, the numbers are compared with values, rather than comparisons between 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) ; i < minLen; i++) {//The number where the number is located var indexa = a.indexOf(lista[i]);var indexb = b.indexOf(listb[i]);//The prefix before the number var prefixa = a.substring(0, indexa);var prefixb = a.substring(0, indexb);//The stringvar stra = lista[i];var strb = listb[i];//The value of the number var numa = parseInt(stra);var numb = parseInt(strb);//If the number's sequence numbers are not equal or the prefix is not equal, it is a case of different prefixes, directly compare if (indexa != indexb || prefixa != prefixb) {return a.localeCompare(b);}else {//The string congruent of the number (stra) === strb) {//If it is the last number, compare the suffix of the number if (i == minLen - 1) {return a.substring(indexa).localeCompare(b.substring(indexb));}//If it is not the last number, loop to the next number and remove the same part before else {a = a.substring(indexa + stra.length);b = b.substring(indexa + stra.length);}}//If the string of the number is not complete, but the values are equal else if (numa == numb) {//Sign up the number of prefixes 0, the more return strb.lastIndexOf(numb + '') - stra.lastIndexOf(numa + '');}else {//If the numbers are not equal, directly compare the size of the numbers return numa - numb;}}}}}How to use, Array.sort(SortLikeWin);
The above is the effect of sorting by name of imitation of windows files based on JS. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!