choose a field type to sort a list

BlitzMax Forums/BlitzMax Beginners Area/choose a field type to sort a list

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.

Create a function, such as compare_y, which specifies what you want to sort against. Then, when you call sortlist, declare that function as the 'CompareFunc'.
Type TPix
	Global pixels_list:TList=CreateList()
  	Field x : Int
  	Field y : Int
	Function Create:TPix (x,y)
		Local p:TPix = New TPix
		p.x = x
		p.y = y
		ListAddLast Pixels_list, p
		Return p
	End Function
	Function Printthem()
		For all:tpix = EachIn pixels_list
		  Print all.y
		Next
	End Function
	Function Compare_y:Int(o1:Object,o2:Object)
		If tpix(o1).y>tpix(o2).y Return 1
		Return 0
	End Function	

End Type
SeedRnd MilliSecs()
For Local xnum=0 To 10
	my:Tpix=TPix.create(xnum,Rand(0,99))
Next
Print "Unsorted"
Tpix.printthem()
SortList(tpix.pixels_list,True,tpix.compare_y)
Print "Sorted"
Tpix.Printthem()