sort()数组顺序与数字 [英] sort() array names sequentially with numbers
问题描述
大家好,不久之后我问如何排序一个字符串数组,这些字符串将会有数字,我希望按顺序数字顺序排列。
例如:
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 would
have 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 but
crashes IE5, actually it pops an alert that the script has been running a
long time and wants me to bail. Can anyone shed some light as to why this
code 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
推荐答案
David a écrit:
David a écrit :
大家好,一会儿我问如何排序一个字符串数组,这些字符串会有数字,我想把它们按顺序排列。
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" ;;
然而,使用下面的常规排序函数进行数字排序...
和...为什么不重命名你的电影es?
myArray [0] =" file01";
myArray [1] =" file02";
myArray [2] =" file03";
myArray [3] =" file04";
myArray [4] =" file05";
myArray [5] =" file06";
myArray [6] =" file07";
myArray [7] =" file08" ;;
myArray [8] =" file09";
myArray [9] =" file10" ;;
myArray [10] =" ; file11" ;;
会修复你的myArray.sort(比较)
----------------- ----------------------
功能比较(a,b){
返回(ab)
} >
myArray.sort(比较);
---------------------------------- -----
按照我不想要的顺序返回数组,并且不一致。
例如:
myArray [0] =" file1" ;;
myArray [1] =" file10" ;;
myArray [2] =" file2";
myArray [3] =" file3";
myArray [4] =" file4";
等。
同样的事情发生在20等...
Hi all, A while back I asked how to sort an array of strings which would
have 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...
and ... why not rename your files ?
myArray[0] = "file01";
myArray[1] = "file02";
myArray[2] = "file03";
myArray[3] = "file04";
myArray[4] = "file05";
myArray[5] = "file06";
myArray[6] = "file07";
myArray[7] = "file08";
myArray[8] = "file09";
myArray[9] = "file10";
myArray[10] = "file11";
would fix your myArray.sort(compare)
---------------------------------------
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..
-
Stephane Moriaux et son [moins] vieux Mac
--
Stephane Moriaux et son [moins] vieux Mac
>并且...为什么不重命名你的文件?
因为他们不会成为我的档案而我无法控制他们的名字
命名,但他们必须按特定顺序排序。
David
> and ... why not rename your files ?
Because they won''t be my files and I will have no control over what they are
named, however they must be sorted in a specific order.
David
David< ri***@dd.com>在消息新闻中写道:NhG_e.11469
David <ri***@dd.com> wrote in message news:NhG_e.11469
这篇关于sort()数组顺序与数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!