Sort 2D list

From Director Online Wiki
Jump to: navigation, search

This function sorts a 2dimentional list by column.

  -------------------------------------------------------------------------------
  -- standard simple bubble sort 
  -- a = input 2 dimensional list list
  -- b = column to sort on
  on Sort2DList a, b 
    if ilk(a) <> #list then return -1
    t = []
    t[b] = 0
    a.add(t)
    repeat with i=a.count down to 0
      flipped = false
      j=0
      repeat while j<i
        j=j+1
        if j+1 < a.count then
          aInt = float(a[j][b])
          bInt = float(a.getat(j+1)[b])
          if aInt > bInt then 
            T = a[j]
            a[j] = a.getat(j+1)
            a[j+1] = T
            flipped = true
          end if
        end if
      end repeat
    end repeat
    a.deleteat(a.count)
    return a
  end