用逗号排序的数字不起作用 [英] sort number with comma not work

查看:82
本文介绍了用逗号排序的数字不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果对此工作进行排序:

var myarray = new Array(10,16,35," 0.1",8,4,22,19,1,22,35,9, 26,38,40);


代码

功能函数1(a,b)

{return a - b}

var order02 = new Array();

order02 = myarray.sort(function1);


但不是0.1我使用0,1(用逗号)不行;


有可能解决吗?


此外,在函数中进行排序,什么差价

这个

函数function1(a,b)

{return a - b}

这个

函数function2(a,b){

if(ab){return 1; }

if(a == b){return 0; }

if(a< b){return -1; }

}


如果我使用0.1,它们都可以正常工作;

如果我使用0,1(用逗号)

他们工作不同(不同的结果)和两个

无论什么都不行。



------ ----

解决方案

artev于2006年9月9日在comp.lang.javascript中写道
< blockquote class =post_quotes>
如果对此工作进行排序:

var myarray = new Array(10,16,35," 0.1",8,4,22,19, 1,22,35,9,26,38,40);


代码

功能函数1(a,b)

{return a - b}

var order02 = new Array();

order02 = myarray.sort(function1);


而不是0.1我用0,1(用逗号)不起作用;


有可能解决吗?


此外,在函数中进行排序,有什么区别

函数函数1(a,b)

{return a - b}

和这个

函数function2(a,b) {

if(a b){return 1; }

if(a == b){return 0; }

if(a< b){return -1; }


如果我使用0.1,它们都可以正常工作;

如果我使用0,1(用逗号)

他们工作不同(不同的结果)和两个

无论什么都不行。



0,1不被Javascript识别为数字。


尝试[未经测试]:


函数function1(a,b){

返回a.replace(/,/,''。'') - b.replace(/,/,'' 。'')

}


-

Evertjan。

荷兰。

(请将x''es更改为我的电子邮件地址中的点数)


Il 09 Sep 2006 11:51:38 GMT,Evertjan。 ha scritto:


artev于2006年9月9日在comp.lang.javascript中写道


>如果对此工作进行排序:
var myarray = new Array(10,16,35," 0.1",8,4,22,19,1,22,35,9, 26,38,40);

代码
函数function1(a,b)
{return a - b}
var order02 = new Array();
order02 = myarray.sort(function1);

但不是0.1我用0,1(用逗号)不行;

有可能解决吗?


0,1不被Javascript识别为数字。


试试[未经测试]:


函数function1(a,b){

返回a.replace(/,/,''。'') - b。替换(/,/,''。'')

}



0,1不是被Javascript识别为数字。



如果问题是这个,那么我必须更改数组中的elemnts值(首先是

以传递函数排序)。 />
我已经测试过,但没有工作;

我认为正则表达式

string.replace(/,/,''。'');

不正确。


是这样吗?


artev于2006年9月9日写道在comp.lang.javascript


Il 09 Sep 2006 11:51:38 GMT,Evertjan。 ha scritto:


> artev于2006年9月9日在comp.lang.javascript中写道


>>如果对此工作进行排序:
var myarray = new Array(10,16,35," 0.1",8,4,22,19,1,22, 35,9,26,38,40);

带代码
功能函数1(a,b)
{return a - b}
var order02 = new Array();
order02 = myarray.sort(function1);

但不是0.1我用0,1(用逗号)不起作用;

是可以解决?


> 0,1不被Javascript识别为数字。

尝试[未测试]:

函数function1(a,b){
返回a.replace(/,/,''。'') - b.replace(/,/,''。' ')
}




> 0,1不被Javascript识别为数字。



如果问题是这个



你测试过吗?

如果所以你不必说''如果''。


alert(6 *''1,1'')// NaN

alert( 6 *''1.1'')// 6.600000etc。


然后我必须更改数组中的elemnts值

(首先是通过功能排序)。

我已经测试过,但没有工作;



不工作在这个NG中是不可接受的,

你必须解释发生了什么以及你对dbug做了什么。


我认为正则表达式

string.replace(/,/,''。'');

不正确。



''我想''????

这不是猜谜游戏,而是认真的编程。

你测试它是不正确的吗?


alert(+''1,1'')// NaN

alert(+''1, 1''。替换(/,/,''。''))// 1.1


我认为你期待我们做肮脏的工作,

而实际上你给了建议

并且应该自己做测试。

-

Evertjan。

荷兰。

(请在我的电子邮件地址中将x'变为点数)


If sort this work:
var myarray= new Array(10,16,35,"0.1",8,4,22,19,1,22,35,9,26,38,40) ;

with code
function function1(a,b)
{return a - b}
var order02=new Array();
order02=myarray.sort(function1);

but instead of 0.1 I use 0,1 (with comma) not work;

Is possible to solve?

Further, in the function for to sort, what is the difference
between this
function function1(a,b)
{return a - b}
and this
function function2(a, b) {
if (a b) { return 1; }
if (a == b) { return 0; }
if (a < b) { return -1; }
}

If I use 0.1 they work good both;
If I use 0,1 (with comma)
they work dissimilar (different result) and both
whatever not work.


----------

解决方案

artev wrote on 09 sep 2006 in comp.lang.javascript:

If sort this work:
var myarray= new Array(10,16,35,"0.1",8,4,22,19,1,22,35,9,26,38,40) ;

with code
function function1(a,b)
{return a - b}
var order02=new Array();
order02=myarray.sort(function1);

but instead of 0.1 I use 0,1 (with comma) not work;

Is possible to solve?

Further, in the function for to sort, what is the difference
between this
function function1(a,b)
{return a - b}
and this
function function2(a, b) {
if (a b) { return 1; }
if (a == b) { return 0; }
if (a < b) { return -1; }
}

If I use 0.1 they work good both;
If I use 0,1 (with comma)
they work dissimilar (different result) and both
whatever not work.

0,1 is not recognized by Javascript as a number.

try [not tested]:

function function1(a,b) {
return a.replace(/,/,''.'') - b.replace(/,/,''.'')
}

--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)


Il 09 Sep 2006 11:51:38 GMT, Evertjan. ha scritto:

artev wrote on 09 sep 2006 in comp.lang.javascript:

>If sort this work:
var myarray= new Array(10,16,35,"0.1",8,4,22,19,1,22,35,9,26,38,40) ;

with code
function function1(a,b)
{return a - b}
var order02=new Array();
order02=myarray.sort(function1);

but instead of 0.1 I use 0,1 (with comma) not work;

Is possible to solve?

0,1 is not recognized by Javascript as a number.

try [not tested]:

function function1(a,b) {
return a.replace(/,/,''.'') - b.replace(/,/,''.'')
}


0,1 is not recognized by Javascript as a number.

If problem is this then I must change the elemnts value in the array (first
to pass at the function sort).
I have tested, but not work;
I think the regular expression
string.replace(/,/,''.'');
isn''t correct.

Is so?


artev wrote on 09 sep 2006 in comp.lang.javascript:

Il 09 Sep 2006 11:51:38 GMT, Evertjan. ha scritto:

>artev wrote on 09 sep 2006 in comp.lang.javascript:

>>If sort this work:
var myarray= new Array(10,16,35,"0.1",8,4,22,19,1,22,35,9,26,38,40) ;

with code
function function1(a,b)
{return a - b}
var order02=new Array();
order02=myarray.sort(function1);

but instead of 0.1 I use 0,1 (with comma) not work;

Is possible to solve?

>0,1 is not recognized by Javascript as a number.

try [not tested]:

function function1(a,b) {
return a.replace(/,/,''.'') - b.replace(/,/,''.'')
}



>0,1 is not recognized by Javascript as a number.

If problem is this

Did you test it?
If so you would not have to say ''If''.

alert( 6 * ''1,1'') // NaN
alert( 6 * ''1.1'') // 6.600000etc.

then I must change the elemnts value in the array
(first to pass at the function sort).
I have tested, but not work;

"not work" is not acceptable in this NG,
you will have to explain what happens and what you did to dbug.

I think the regular expression
string.replace(/,/,''.'');
isn''t correct.

''I think'' ????
This is no guessing game but serious programming.
Did you test it to be incorrect?

alert( +''1,1'') // NaN
alert( +''1,1''.replace(/,/,''.'')) // 1.1

I think you are expecting us to do the dirty work,
while in fact you are given advice
and should do the testing yourself.
--
Evertjan.
The Netherlands.
(Please change the x''es to dots in my emailaddress)


这篇关于用逗号排序的数字不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆