dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: sub sorting multi-dimesional arrays

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    24

    sub sorting multi-dimesional arrays

    oh hai,

    I have my 2D array right, and it kinda would look like this:

    [
    [250,350,0,11],
    [250,250,0,12],
    [250,225,1,2],
    [250,175,1,1],
    [300,150,0,5],
    [400,10,0,7],
    [400,250,0,8],
    [450,350,0,10]
    ]

    So as you can see I have already ordered my array by the first element in the sub arrays, what i need to do now is sub order said array so it will end up like:

    [250,175,1,1],
    [250,225,1,2],
    [250,250,0,12],
    [250,350,0,11],
    [300,150,0,5],
    [400,10,0,7],
    [400,250,0,8],
    [450,350,0,10]

    Cheers

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    PHP Code:
    for each(var sub:Array in outerArray){
        
    sub.sort(Array.NUMERIC);

    Please use [php] or [code] tags, and mark your threads resolved 8)

  3. #3
    Junior Member
    Join Date
    May 2009
    Posts
    24
    Kinda... but not what I want. That orders inside the sub-arrays numericaly I need to order the main array internally.

    returns
    Code:
    [0,11,250,350]
    [0,12,250,250]
    [1,2,225,250]
    [1,1,175,250]
    [0,5,150,300]
    [0,7,10,400]
    [0,8,250,400]
    [0,10,350,450]
    instead of what I said above

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    It looks like you want one sort that compares a[1] to b[1] only if a[0] == b[0]. So what you really want is not a second sort, but a single sort that is more sensitive. Try this as the function to pass to sort:
    Code:
    function compareByCol(a:Array, b:Array):int{
      var i:int = 0;
      while ((i < a.length) && (i < b.length)){
        var diff:int = a[i] - b[i];
        if (diff != 0){ //they differ
          return diff; //return number representing whether a or b is less
        }
        i++;
      }
      //if we get here, we've exhausted at least one array.  treat the shorter one as lesser
      return a.length - b.length
    }

  5. #5
    Junior Member
    Join Date
    May 2009
    Posts
    24
    AMAZING! thanks heaps! works perfectly XD

Tags for this Thread

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