本文介绍了sort()数组顺序与数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好,不久之后我问如何排序一个字符串数组,这些字符串将会有数字,我希望按顺序数字顺序排列。 例如: myArray [0] =" file1" ;; myArray [1] =" ; file2" ;; myArray [2] =" file3"; myArray [3] =" file4"; myArray [ 4] =" file5"; myArray [5] =" file6"; myArray [6] =" file7"; myArray [7] =" file8"; myArray [8] =" file9"; myArray [9] =" file10"; myArray [10] =" file11"; 等... 但是使用普通排序功能下面的数字排序...... ----------------------------------- ---- 功能比较(a,b){ 返回(ab) } myArray.sort(比较); ------------------------------- -------- 按照我不想要的顺序返回数组并且不一致。 例如: myArray [0] =" file1"; myArray [1] =" file10"; myArray [2] =" file2" ;; myArray [3] =" file3"; myArray [4] =" file4"; 等.. 当它达到20等时会发生同样的事情。 我在下面给出了这个代码,它在大多数现代浏览器中都很有用但是 崩溃IE5,实际上它会弹出一个警告,说该脚本已经运行了很长时间并且希望我保释。任何人都可以解释为什么这个 代码失败或知道另一种方法来实现这个目标吗? 函数cmp(a,b){ 返回(b } 函数numCmp(a,b){ var re1 = /(\d +)| \ D + / g; var re2 = /(\d +)| \ D + / g; re1.lastIndex = 0; re2.lastIndex = 0; var res = 0; do {match1 = re1.exec(a); match2 = re2.exec(b); if(match1){if(match2){ if(match1 [1]){ if (match2 [1]){ res = Number(match1 [1]) - 数字(match2 [1])|| cmp(match1 [0],match2 [0]); } else {res = -1;} } else {if(match2 [1]){res = 1; } else {res = cmp(match1 [0],match2 [0]);}} } else {res = 1;}} else {if(match2){res = - 1;} else {res = 0;休息; } } } while(res == 0); 返回res; } - 谢谢David Hi all, A while back I asked how to sort an array of strings which wouldhave numerals and I wanted to put them in sequential numerical order. For example: myArray[0] = "file1";myArray[1] = "file2";myArray[2] = "file3";myArray[3] = "file4";myArray[4] = "file5";myArray[5] = "file6";myArray[6] = "file7";myArray[7] = "file8";myArray[8] = "file9";myArray[9] = "file10";myArray[10] = "file11"; etc... However using the normal sort function below for numerical sorting...---------------------------------------function compare(a,b){return(a-b)} myArray.sort(compare);---------------------------------------returns the array in an order I do not want and is not consistent. For example:myArray[0] = "file1";myArray[1] = "file10";myArray[2] = "file2";myArray[3] = "file3";myArray[4] = "file4";etc.. Same thing happens when it gets to 20 etc.. I was given this code below which works great in most modern browsers butcrashes IE5, actually it pops an alert that the script has been running along time and wants me to bail. Can anyone shed some light as to why thiscode fails or know of another way to accomplish this?function cmp(a,b) {return (b<a)-(a<b);}function numCmp(a,b) {var re1 = /(\d+)|\D+/g;var re2 = /(\d+)|\D+/g;re1.lastIndex = 0;re2.lastIndex=0;var res = 0;do{ match1 = re1.exec(a);match2 = re2.exec(b);if(match1){if (match2){if(match1[1]) {if(match2[1]) {res = Number(match1[1]) - Number(match2[1]) ||cmp(match1[0],match2[0]);}else{res = -1;}}else{if(match2[1]){res = 1;}else{res = cmp(match1[0],match2[0]);}}}else{res = 1;}}else{if(match2){res = -1;}else{res = 0; break;}}} while(res == 0);return res;}--Thanks David推荐答案 - Stephane Moriaux et son [moins] vieux Mac --Stephane Moriaux et son [moins] vieux Mac 这篇关于sort()数组顺序与数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 14:57