8 Comments

  1. bighippo
    October 2, 2008 @ 9:48 pm

    whoa thanks! bighelp!

    Reply

  2. Larry Battle
    October 10, 2010 @ 2:28 pm

    Fastest way to sort numbers in javascript.
    Here’s my code.

    function insertionSort3 (sortMe){
    var Len = sortMe.length, i = -1, j, tmp;

    while( Len-- ){
    tmp = sortMe[++i];
    j = i;
    while( j-- && sortMe[j] > tmp ) {
    sortMe[j+1]=sortMe[j];
    }
    sortMe[j+1]=tmp;
    }
    }

    Here’s a test. You need to use Firefox and Firebug to see the results.

    var arr = [];

    var getArrOfRandomNumbers = function( howMany, largestValue ){
    var arr = [];
    howMany = ( howMany > 0 ) ? howMany : 0;
    while( howMany-- ){
    arr.push( Math.floor( Math.random() * largestValue ));
    }
    return arr;
    };

    arr = getArrOfRandomNumbers( 1000, 110 );

    console.time( "insertionSort" );
    insertionSort3( arr );
    console.timeEnd( "insertionSort" );

    arr = getArrOfRandomNumbers( 1000, 110 );

    console.time( "arr.sort()" );
    arr = arr.sort( function(a,b){return a-b;});
    console.timeEnd( "arr.sort()" );


    -Larry Battle

    Reply

  3. ZQ
    December 21, 2010 @ 4:27 pm

    I believe this post is incorrect. The explanation above should read…

    – for a positive value (a number greater than 0), ‘b’ will be put before ‘a’.
    – for a negative value (a number less than 0), ‘a’ will be put before ‘b’.
    – for 0 (meaning ‘a’ and ‘b’ are equal), then the positions of these two elements will not change in the sorted array

    ZQ

    Reply

  4. javascript coders-please help - DesignersTalk
    September 14, 2011 @ 10:25 am

    […] Google is your friend second thing I found when I searched sorting numbers in javascript was this: Sort numbers in Javascript array […]

    Reply

  5. Priyanka
    June 14, 2012 @ 10:13 pm

    Thank you…was a big help…

    Reply

  6. abc
    July 1, 2014 @ 3:46 am

    kpo_attendance

    Reply

  7. facebook farmville 2 hack free farm bucks and coins cheat
    August 1, 2014 @ 2:43 pm

    This is really interesting, You are an excessively skilled blogger.
    I have joined your rss feed and look ahead
    to looking for more of your excellent post. Also, I have shared your website in my social networks

    Reply

  8. Cho-Ching
    May 8, 2017 @ 12:04 am

    Thanks a lot!!!!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

I accept that my given data and my IP address is sent to a server in the USA only for the purpose of spam prevention through the Akismet program.More information on Akismet and GDPR.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

privacy policy