473,406 Members | 2,293 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

sort number with comma not work

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
13 3173
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
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
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
>'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
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
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
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
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
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
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
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
>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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Martin Lucas-Smith | last post by:
I am trying to take a string and split it into a filename and a number where the number is what follows the *last* instance of a comma in that string. Split and explode won't work because if the...
3
by: happy | last post by:
/* Book name : The prodessional programmers guide to C File name : E:\programs\tc\iti01\ch09\main\01setupm.c Program discription: file setuping -up -Version 01-ver01-W Logic ...
122
by: Einar | last post by:
Hi, I wonder if there is a nice bit twiddling hack to compare a large number of variables? If you first store them in an array, you can do: for (i = 0; i < n; i++) { if (array != value) {...
9
by: Steve Wasser | last post by:
I need to sort a two-dimensional array. Each day I process a file with 9 comma-delimited fields, and varying amount of records (lines). I want to pull in each line, split it according to the comma...
1
by: nate axtell | last post by:
In VB .NET I load the contents of an Excel or comma seperated values file into a dataGrid (via a datatable). One of the columns has a comma in the name of the column. So for the comma separated...
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
4
by: Tony WONG | last post by:
i have a number of forms with fax numbers to come up into arrays and then combine to string. after that i design the flow 1. break the string to array now the string looks like this...
43
by: Xancatal | last post by:
Hey everybody. I need help on this one. I need to verify that a number entered by a user is not either a negative number (-100.00), or an alphabet (a, b, c, X, Y) as well as other number other than...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.