Array sort function sorts on chars not numbers ... help ! how to sort numbers

Discussion in 'Javascript' started by GIMME, Jul 22, 2004.

  1. GIMME

    GIMME Guest

    My question is ... How do I sort an Array on numeric, not character values ?

    In the example below, after sorting the contents are 1,10,2,3 .

    How do I get the contents to be 1,2,3,10 ?

    Thanks,

    em

    <html>
    <head>
    <script>
    function TestSort() {
    var nums = new Array(0);
    nums[0] = 1;
    nums[1] = 2;
    nums[2] = 10;
    nums[3] = 3;
    nums.sort();
    for ( i = 0 ; i < 4 ; i++ ) {
    alert(nums);
    }
    return true;
    }
    </script>
    </head>
    <body bgcolor="lightblue">
    <input type="button" onclick="TestSort();" value="Click Me">
    </body>
    </html>
    GIMME, Jul 22, 2004
    #1
    1. Advertising

  2. GIMME

    McKirahan Guest

    "GIMME" <> wrote in message
    news:...
    > My question is ... How do I sort an Array on numeric, not character values

    ?
    >
    > In the example below, after sorting the contents are 1,10,2,3 .
    >
    > How do I get the contents to be 1,2,3,10 ?
    >
    > Thanks,
    >
    > em
    >
    > <html>
    > <head>
    > <script>
    > function TestSort() {
    > var nums = new Array(0);
    > nums[0] = 1;
    > nums[1] = 2;
    > nums[2] = 10;
    > nums[3] = 3;
    > nums.sort();
    > for ( i = 0 ; i < 4 ; i++ ) {
    > alert(nums);
    > }
    > return true;
    > }
    > </script>
    > </head>
    > <body bgcolor="lightblue">
    > <input type="button" onclick="TestSort();" value="Click Me">
    > </body>
    > </html>


    Here's a solution; watch for word-wrap.


    <html>
    <head>
    <title>sortnum.htm</title>
    <script type="text/javascript">
    function TestSort() {
    var nums = new Array(0);
    nums[0] = 1;
    nums[1] = 2;
    nums[2] = 10;
    nums[3] = 3;
    nums.sort();
    for (var i=0; i<nums.length; i++) {
    nums = (10000 + nums);
    }
    nums.sort();
    var numx = "";
    for (var j=0; j<nums.length; j++) {
    numx += (nums[j] - 10000) + "\n";
    }
    alert(numx);
    }
    </script>
    </head>
    <body onload="TestSort()">
    </body>
    </html>
    McKirahan, Jul 22, 2004
    #2
    1. Advertising

  3. GIMME

    Lee Guest

    GIMME said:
    >
    >My question is ... How do I sort an Array on numeric, not character values ?


    It's hard to believe that you could find any documentation on Array.sort()
    that doesn't include an example of how to sort numerically.
    There's one at this link:

    http://tinyurl.com/4ohf4

    <http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/array.html#1196882>
    Lee, Jul 22, 2004
    #3
  4. GIMME

    Ivo Guest

    "GIMME" wrote
    > My question is ... How do I sort an Array on numeric, not character values

    ?
    >
    > In the example below, after sorting the contents are 1,10,2,3 .
    >
    > How do I get the contents to be 1,2,3,10 ?


    Two steps. See inserted lines:

    > <html>
    > <head>
    > <script>
    > function TestSort() {
    > var nums = new Array(0);
    > nums[0] = 1;
    > nums[1] = 2;
    > nums[2] = 10;
    > nums[3] = 3;
    > nums.sort();


    1. Make that:
    nums.sort(sortnumeric);

    > for ( i = 0 ; i < 4 ; i++ ) {
    > alert(nums);
    > }
    > return true;
    > }


    2. And add:
    function sortnumeric(a,b){ return parseFloat(a)-parseFloat(b); }

    > </script>
    > </head>
    > <body bgcolor="lightblue">
    > <input type="button" onclick="TestSort();" value="Click Me">
    > </body>
    > </html>


    HTH
    Ivo
    Ivo, Jul 22, 2004
    #4
  5. JRS: In article <40ff0d6f$0$149$>, dated Thu,
    22 Jul 2004 02:44:28, seen in news:comp.lang.javascript, Ivo
    <> posted :
    >"GIMME" wrote
    >> My question is ... How do I sort an Array on numeric, not character values


    That might be FAQ-worthy; I've seen it quite a few times. There's
    obvious generalisation to other types of sort, e.g. dates using getTime
    or valueOf, both of which should be quick.


    >2. And add:
    >function sortnumeric(a,b){ return parseFloat(a)-parseFloat(b); }


    That requires, for each of the ~ o(N ln N) comparisons, two conversions
    of number to string and two conversions of string to number. Only
    subtraction is needed.

    I can believe that adding a large constant to each number could be
    quicker, for large sorts, since it avoids calling a user-defined
    function.

    Someone tell ECMA to add a numericSort method to anything that has a
    sort method. The chief benefit would be that, eventually, it would
    appear in documentation, help files, and books; and it should be a
    little quicker.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
    Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
    PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
    Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
    Dr John Stockton, Jul 22, 2004
    #5
  6. Re: Array sort function sorts on chars not numbers ... help ! howto sort numbers

    GIMME wrote:
    > My question is ... How do I sort an Array on numeric, not
    > character values ?


    function numSort(a, b)
    {
    return a - b;
    }

    var a = new Array(...);
    a.sort(numSort);

    It is described in detail in the Client-side JavaScript 1.3+
    References, if you would have cared to RTFFAQ/RTFM prior to
    posting you would have known.


    PointedEars
    Thomas 'PointedEars' Lahn, Jul 26, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Srikanth Mandava
    Replies:
    1
    Views:
    394
    Michael Hudson
    Feb 19, 2004
  2. Replies:
    6
    Views:
    376
  3. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,285
    Tim Rentsch
    Sep 23, 2005
  4. Hongyu
    Replies:
    9
    Views:
    902
    James Kanze
    Aug 8, 2008
  5. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    226
    Dan Rogers
    Nov 16, 2004
Loading...

Share This Page