tList.Sort()

BlitzMax Forums/BlitzMax Programming/tList.Sort()

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

There are two ways.
1) Override the Compare method, return -1 if self < object, +1 if self > object or 0 if they are equal. However, this creates problems with TList, so with version 1.20, method #2 was introduced. :)
2) create a function that takes two objects and return -1 if o1 < o2, +1 if o1 > o2, and 0 if they are equal.

Type MyType
	Field name:String
	Field age:Int
End Type

Local MyList:TList = CreateList()

For Local t = 0 To 10
	MyPerson:MyType = New MyType
	MyPerson.name = Chr(Rand(65,90))+Chr(Rand(65,90))+Chr(Rand(65,90))
	MyPerson.age = Rand(18,65)
	ListAddLast(MyList,MyPerson)
Next

Print "unsorted...."
For MyPerson:MyType = EachIn MyList
	Print "Name: "+MyPerson.name+" Age: "+MyPerson.age
Next

SortList(MyList,True,MySortFunction)
Print "~nSorted...."
For MyPerson:MyType = EachIn MyList
	Print "Name: "+MyPerson.name+" Age: "+MyPerson.age
Next

Function MySortFunction:Int(o1:Object, o2:Object)
	Return MyType(o1).age-MyType(o2).age
End Function