Sorting any field

BlitzMax Forums/BlitzMax Beginners Area/Sorting any field

Type Unit
     Global unitlist:Tlist=CreateList()
     Field X:Float,Y:Float
     Field Number:Int

     Method New()
          Number=Rand(1000)
          ListAddLast UnitList,Self
     End Method
     Method compare(myobject:Object)
          s:Unit = Unit(myobject)
          If Not s Then Return 1
          Return number - s.number
     End Method
End Type
SeedRnd MilliSecs()
For x = 1 To 100
  my:Unit = New unit
Next
   
SortList unit.unitlist
   
   ' Check to see if it is sorted correctly
For s:unit = EachIn unit.unitlist
    Print s.number
Next