By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,481 Members | 1,713 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,481 IT Pros & Developers. It's quick & easy.

sort number with comma not work

P: n/a
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.


----------
Sep 9 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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)
Sep 9 '06 #2

P: n/a
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?
Sep 9 '06 #3

P: n/a
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)
Sep 9 '06 #4

P: n/a
>'I think' ????
>This is no guessing game but serious programming.
Did you test it to be incorrect?
Excuse I does one not good test (think)

var mytest= new Array(10,16,35,"0,1",8,4,22,19,1,22,35,9,26,38,40) ;
for (var i = 0; i<mytest.length; i++)
{mytest[i]=mytest[i].replace(/,/,'.');
alert( mytest[i]);
}

not function;

but your code not work also
function function1(a,b)
{
return a.replace(/,/,'.') - b.replace(/,/,'.')
}
therefore I have thought mistaking that the problem could be expression
regular;
now think is the array's shape.
Sep 9 '06 #5

P: n/a
artev wrote on 09 sep 2006 in comp.lang.javascript:
>>'I think' ????
This is no guessing game but serious programming.
Did you test it to be incorrect?

Excuse I does one not good test (think)

var mytest= new Array(10,16,35,"0,1",8,4,22,19,1,22,35,9,26,38,40) ;
for (var i = 0; i<mytest.length; i++)
{mytest[i]=mytest[i].replace(/,/,'.');
alert( mytest[i]);
}

not function;
try this:

<script type='text/javascript'>

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

for (var i = 0; i<mytest.length; i++)
alert(mytest[i].toString().replace(/,/,'.'));

// tested

</script>
but your code not work also
function function1(a,b)
{
return a.replace(/,/,'.') - b.replace(/,/,'.')
return a.toString().replace(/,/,'.') - b.toString().replace(/,/,'.')

// not tested
// the - minus will convert the strings back to numbers
>}
therefore I have thought mistaking that the problem could be expression
regular;
now think is the array's shape.


--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 9 '06 #6

P: n/a
Il 09 Sep 2006 17:31:43 GMT, Evertjan. ha scritto:
artev wrote on 09 sep 2006 in comp.lang.javascript:
>>>'I think' ????
try this:

<script type='text/javascript'>

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

for (var i = 0; i<mytest.length; i++)
alert(mytest[i].toString().replace(/,/,'.'));
ok work;
I first change the array with your suggestion:
mytest[i].toString().replace(/,/,'.');
and after I use this function
function function1(a,b)
{ return a - b }
in the sort:
mytest.sort(function1);
but if I change function:
function function2(a, b)
{
if (a b) { return 1; }
if (a == b) { return 0; }
if (a < b) { return -1; }
}

not work more (the sort isn't correct);
Only for curiosity:the two function aren't equal?

-------
other, is preferable (for example, for the time of execution)
to use the transformation, comma to dot,
first of use the array in the sort or (how your example) directly inner the
function?

thanks for all the clarifications
Sep 9 '06 #7

P: n/a
artev wrote on 09 sep 2006 in comp.lang.javascript:
but if I change function:
function function2(a, b)
{
if (a b) { return 1; }
if (a == b) { return 0; }
if (a < b) { return -1; }
}

not work more (the sort isn't correct);
Only for curiosity:the two function aren't equal?

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

can be speeded up by removing superfluous parts,
the {} and especially the last if,
think of the logic!

if (a b)
return 1;
if (a == b)
return 0;
return -1;

however here you need to ensure the values
are numerical, not numbers.

===========

return a-b ;

or better

return
a.toString().replace(/,/,'.')-b.toString().replace(/,/,'.');

automagically converts to numbers, since subsraction does that.

this is equivalent in the sense that the sort function needs only
zero, or any positive number, or any negative number.
Those last two need NOT be 1 and -1 per se.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 9 '06 #8

P: n/a
JRS: In article <Xn********************@194.109.133.242>, dated Sat, 9
Sep 2006 17:31:43 remote, seen in news:comp.lang.javascript, Evertjan.
<ex**************@interxnl.netposted :
>function function1(a,b)
{
return a.replace(/,/,'.') - b.replace(/,/,'.')

return a.toString().replace(/,/,'.') - b.toString().replace(/,/,'.')
As sorting is a comparatively slow operation, making O(N) comparisons,
then if the array may contain many plain Numbers it would IMHO be better
to convert only the Strings - unless testing for type is slow.

If possible, the OP should do an initial pass, O(N), converting all
Strings to Numbers.

If the original form must be preserved, and N may be large, one could do
an O(N) pass to replace every element by an Object with two parts : X,
the original value and Y, the Number form. Then sort >O(N) with a
simple comparison function body like return a.Y - b.Y and then do
another O(N) pass replacing the Objects with their X fields.

In any case, if speed may matter, test speed.
###
OP : when repeating a question which you have already posted elsewhere,
you should say so. Your article in this newsgroup would have been
easier to understand if read in conjunction with your "ordinare non
funziona se ho numero con virgola".

For reasonably short articles, such as yours, it is better to cross-post
one article in both languages to both newsgroups.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Sep 9 '06 #9

P: n/a
Dr John Stockton wrote on 10 sep 2006 in comp.lang.javascript:
In any case, if speed may matter, test speed.
This a drug related advice?
As sorting is a comparatively slow operation, making O(N) comparisons,
then if the array may contain many plain Numbers it would IMHO be better
to convert only the Strings - unless testing for type is slow.
Having a mixed array in javascript is not yust a mixed blessing
but a wrong way to start with.

The conversion should be done while building the array.

Though over here the decimal character is a comma,
this should in a javascript environment only be
accepted as input or used for display.
Not as a variable or database value.

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

P: n/a
however here you need to ensure the values
are numerical, not numbers.
what is difference?

other:
you how manage to sort a list of number where you have
some number with comma and others with dot?

Sep 10 '06 #11

P: n/a
artev wrote on 10 sep 2006 in comp.lang.javascript:
>however here you need to ensure the values
are numerical, not numbers.

what is difference?
slip of the keyboard, I ment numerical, not a string.

other:
you how manage to sort a list of number where you have
some number with comma and others with dot?
That is where this thread was all about, please reread.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 10 '06 #12

P: n/a
>other:
you how manage to sort a list of number where you have
some number with comma and others with dot?

That is where this thread was all about, please reread.
I want ask this really:
if you have this price
10,000.52 $
100.25
1,000

you can use both (comma and dot)?

or equivalent in euro:
10.000,52 Euro
100,25
1.000

How is possible to resolve similar list; when there are
both?
Sep 11 '06 #13

P: n/a
artev wrote on 11 sep 2006 in comp.lang.javascript:
>>other:
you how manage to sort a list of number where you have
some number with comma and others with dot?

That is where this thread was all about, please reread.

I want ask this really:
if you have this price
10,000.52 $
100.25
1,000

you can use both (comma and dot)?

or equivalent in euro:
10.000,52 Euro
100,25
1.000
Euro's are written the US$ way in several EU countries!
How is possible to resolve similar list; when there are
both?
That is possible but cumbersome.
you will have to do a lot of string crunching,
preferably with regular expressions.

However:

A list that you want to sort numerically
should contain only numerical values in the javascript sense,
that is integer or floating point numbers.numbers.

The conversion, that should be done on list entry,
can never be complete,
if you don't have a closed definition of possibilities.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Sep 11 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.