排序字母数字 [英] Sorting alphanumeric
问题描述
使用myArray.sort()对以下字母数字排序:
04-273-0001
04-272-0001
04-272-0003
04-272-0001
04-273-0001
结果:
04-272-0001
04-272-0001
04-273-0001
04 -273-0001
04-272-0003< -
^
我不能假设标准格式也不能我假设数组中可能包含字母数字的
字符。当这些值包含任何字母数字字符时,我如何准确地对此进行排序?这是
可能没有创建一个疯狂的层次结构或字符和异常
规则??
谢谢。
鲍勃 - 塞纳公司
Sorting the following alphanumerics using myArray.sort():
04-273-0001
04-272-0001
04-272-0003
04-272-0001
04-273-0001
Results in:
04-272-0001
04-272-0001
04-273-0001
04-273-0001
04-272-0003 <--
^
I cannot assume a standard format nor can I assume what alphanumeric
characters might be in the array. How do I sort this accuratly when
these values could contain any alpha numeric character? Is this
possible without creating a crazy hierarchy or characters and exception
rules??
Thanks.
Bob - Cerner Corp.
推荐答案
Bob于2004年12月8日在comp.lang.javascript中写道 :
Bob wrote on 08 dec 2004 in comp.lang.javascript:
使用myArray.sort()对以下字母数字排序:
04-273-0001
04-272-0001
04-272-0003
04-272-0001
04-273-0001
结果:
04-272-0001
04-272-0001
04-273-0001
04-272-0003< -
Sorting the following alphanumerics using myArray.sort():
04-273-0001
04-272-0001
04-272-0003
04-272-0001
04-273-0001
Results in:
04-272-0001
04-272-0001
04-273-0001
04-273-0001
04-272-0003 <--
>
这是数字排序:
4-272-1 = -269
4-273-1 = -270
4-272-3 = -271
-
Evertjan。
荷兰。
(用我的电子邮件地址替换所有带点的十字架)
That is numeric sorting:
4-272-1 = -269
4-273-1 = -270
4-272-3 = -271
--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)
Bob写道:
排序以下字母数字s使用myArray.sort():
04-273-0001
04-272-0001
04-272-0003
04-272-0001 /> 04-273-0001
结果:
04-272-0001
04-272-0001
04-273-0001
04-273-0001
04-272-0003< -
^
我不能假设标准格式也不能假设是什么字母数字
字符可能在数组中。当这些值包含任何字母数字字符时,如何准确地对此进行排序?这是否可能没有创建一个疯狂的层次结构或字符和异常
规则??
Sorting the following alphanumerics using myArray.sort():
04-273-0001
04-272-0001
04-272-0003
04-272-0001
04-273-0001
Results in:
04-272-0001
04-272-0001
04-273-0001
04-273-0001
04-272-0003 <--
^
I cannot assume a standard format nor can I assume what alphanumeric
characters might be in the array. How do I sort this accuratly when
these values could contain any alpha numeric character? Is this
possible without creating a crazy hierarchy or characters and exception
rules??
你将不得不推出自己的排序功能:
A = [" 04-273-0001"," 04-272-0001"," 04-272-0003"," 04-272-0001"," 04-273-0001"]
函数bobSort(a,b){
c = Number(a.split(" - ")[0 ])
d = Number(b.split(" - ")[0])
if(c == d){
c = Number(a.split(" - ")[1])
d = Number(b.split(" - ")[1])
}
if(c == d){
c = Number(a.split(" - ")[2])
d = Number(b.split(" - ")[2])
}
返回cd
}
alert(A.sort(bobSort))
以下格式可能更优越:
c = parseInt(a.split(" - ")[0],10)
d = parseInt(b.split(" - ")[0],10)
....
Mick
You''ll have to roll your own sort function:
A= ["04-273-0001","04-272-0001","04-272-0003","04-272-0001","04-273-0001"]
function bobSort(a,b){
c=Number(a.split("-")[0])
d=Number(b.split("-")[0])
if (c==d){
c=Number(a.split("-")[1])
d=Number(b.split("-")[1])
}
if (c==d){
c=Number(a.split("-")[2])
d=Number(b.split("-")[2])
}
return c-d
}
alert(A.sort(bobSort))
The following format may be superior, though:
c=parseInt(a.split("-")[0],10)
d=parseInt(b.split("-")[0],10)
....
Mick
Mick White写道:
[...]
Mick White wrote:
[...]
你必须自己动手排序功能:
You''ll have to roll your own sort function:
[...]
一个好的开始Mick让我思考。我不相信JavaScript
没有一个适用于字母数字字符串的通用排序。所以我
对您的代码进行了破解,并提出了以下内容。我对世界的贡献
将启动通用排序功能。
是否可以修改非数字前面的数字排序以适应
使用ASCII代码进行所有比较或通过更改charCodeAt
行轻微添加或减去常数,或乘以-1;
我的脚本修改处理任何格式字符串。要处理
数字和非数字,我将非数字更改为ASCII码,
将其与单位数进行比较。不是很好,但它确实排序好 - 告诫
以下。
如果排序的字符用完了比较,那就应该放
最短的一个提前。不同的浏览器需要
不同的返回值:Safari需要-1,Firefox需要0.我不知道
如何区分使用特征检测 - 或者我应该是返回
其他什么?
此外,这会导致不同浏览器的排序顺序不同
(arggghh)。
我到目前为止得到了它,而不是大师。一个改进是
两种类型:sortAsNum()和sortAsChar()。
测试结果(全部在Mac上):
Safari:罚款
Camino:罚款
Firefox:需要更改返回-1才能返回0
IE 5.2:失败
Netscape:需要更改返回-1以返回0
Opera:有时需要-1,有时为0取决于是否某些
条目以alphas开头或不。
<!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN">
< HTML> ;
< HEAD>< title>排序游戏< / title>
< script type =" text / javascript">
函数shortestOf(a,b){
return(a.length< = b.length)? a.length:b.length;
}
函数bobSort(a,b){
var z = shortestOf(a,b) ;
for(var i = 0; i< shortestOf(a,b); i ++){
var c = a.split('''')[i ];
var d = b.split('''')[i];
if(c!= d){
c =(isNaN(c))? c.charCodeAt(0):c;
d =(isNaN(d))? d.charCodeAt(0):d;
var x = cd;
返回c - d;
}
}
返回-1;
}
函数saySort(inp){
var p = inp.split(''\ n'');
alert(p);
alert(p.sort(bobSort).join('' \ n''));
}
< / script>
< / HEAD>
< BODY>
< form action ="">
< textarea cols =" 40"行= QUOT; 20" name =" stuff"> 04-273-0005
040-272-0001
040-272-0003
040-272 -0001
04a-2y2-00c0
04-273-0001
04a-222-00a0
04a -222-00b0
04a-222-00b1
04z-2x2-00a0
04a-2x2-00a0
04a-2y2-00a0
04a-2y2-00c0
04a-2y2-00b0
04a
0ba -cc2&& 8 ## y2-00b0
< / textarea>
< input type =" button"值= QUOT; saySort()" onclick ="
saySort(this.form.stuff.value);
"> < input type =" reset">
< / form>
< / BODY>
< / HTML>
-
Rob
[...]
A good start Mick that got me thinking. I can''t believe JavaScript
doesn''t have a generic sort that works on alpha-numeric strings. So I
had a hack at your code and came up with what''s below. My contribution
to the world is to kick off a generic sort function.
Whether numbers sort ahead of non-numbers can be modified to suit by
making all comparisons using ASCII codes or by changing the charCodeAt
lines slight to add or subtract a constant, or multiply by -1;
My modification of your script handles any format string. To handle
numbers and non-numbers, I change non-numbers to their ASCII code and
compare that to single digits. Not great, but it does sort OK - caveat
below.
If the sort runs out of characters to compare, it should put the
shortest one ahead of the longest. Different browsers require a
different return value: Safari needs -1, Firefox needs 0. I don''t know
how to discriminate using feature detection - or should I be returning
something else?
Also, this causes a difference in the sort order for different browsers
(arggghh).
I got it this far, over to the gurus. An improvement would be to have
two sorts: sortAsNum() and sortAsChar().
Test results (all on Mac):
Safari: fine
Camino: fine
Firefox: need to change return -1 to return 0
IE 5.2: fails
Netscape: need to change return -1 to return 0
Opera: Sometimes needs -1, sometimes 0 depending on whether some
entries start with alphas or not.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD><title>Sort play</title>
<script type="text/javascript">
function shortestOf(a,b) {
return (a.length <= b.length)? a.length:b.length;
}
function bobSort(a,b){
var z = shortestOf(a,b);
for (var i=0; i<shortestOf(a,b); i++) {
var c = a.split('''')[i];
var d = b.split('''')[i];
if ( c != d ) {
c = (isNaN(c))? c.charCodeAt(0):c;
d = (isNaN(d))? d.charCodeAt(0):d;
var x = c-d;
return c - d;
}
}
return -1;
}
function saySort(inp) {
var p = inp.split(''\n'');
alert(p);
alert(p.sort(bobSort).join(''\n''));
}
</script>
</HEAD>
<BODY>
<form action="">
<textarea cols="40" rows="20" name="stuff">04-273-0005
040-272-0001
040-272-0003
040-272-0001
04a-2y2-00c0
04-273-0001
04a-222-00a0
04a-222-00b0
04a-222-00b1
04z-2x2-00a0
04a-2x2-00a0
04a-2y2-00a0
04a-2y2-00c0
04a-2y2-00b0
04a
0ba-cc2&&8##y2-00b0
</textarea>
<input type="button" value="saySort()" onclick="
saySort(this.form.stuff.value);
"> <input type="reset">
</form>
</BODY>
</HTML>
--
Rob
这篇关于排序字母数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!