A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: numeric sort in Flash6?

  1. #1
    Member
    Join Date
    Mar 2002
    Posts
    53

    numeric sort in Flash6?

    I wanted to sort an array by its numeric value but cant make it work. I always get a string-order instead:

    -111.621366801195
    -118.595917758174
    -85.4975083278225
    40.7332060082127

    am using this code:
    Code:
    FOrder.sortOn([0], 16);
    Can anyone tell me how to make it work or if it does work at all in Flash 6?

  2. #2
    Senior Member
    Join Date
    Oct 2004
    Location
    Rio de Janeiro, Brazil
    Posts
    344
    Hi, I am not sure but I think that the array sort method will always consider the array entries as strings. To correctly sort the array, try the following traditional sorting routine:

    PHP Code:
    = new Array();

    a[0] = -111.621366801195;
    a[1] = 40.7332060082127;
    a[2] = -85.4975083278225;
    a[3] = -118.595917758174;

    for (
    i=0;i<a.length;i++)
    {
        for (
    j=i+1;j<a.length;j++)
        {
            if (
    a[i] > a[j])
            {
                
    temp a[i];
                
    a[i] = a[j];
                
    a[j] = temp;
            }
        }
    }

    for (
    z=0;z<a.length;z++)
    {
        
    trace (a[z]);
    }

    stop(); 
    It will produce the following output:

    -118.595917758174
    -111.621366801195
    -85.4975083278225
    40.7332060082127

    PS: This routine ALWAYS will work fine, independently if the entries are numeric values or strings.

  3. #3
    FlashCLUE.com Wizzz's Avatar
    Join Date
    Nov 2005
    Location
    Warsaw Poland
    Posts
    451
    Quote Originally Posted by yankmoura
    Hi, I am not sure but I think that the array sort method will always consider the array entries as strings.
    nope - check out flash manual

    code:
    var arr = [ -111.621366801195, -118.595917758174, -85.4975083278225, 40.7332060082127 ];
    trace( arr);

    arr.sort( function( a,b ){ return a == b ? 0 : (a>b ? 1 : -1 ); } );
    trace( arr );


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center