sort a tlist

BlitzMax Forums/BlitzMax Programming/sort a tlist

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.

Here is an example using Compare
Type Person
	Field name:String
	Field age:Int
	
	Function create:Person(name:String, age:Int)
		Local temp:Person = New Person
		temp.name=name
		temp.age=age
		Return temp
	End Function

	Method Compare(p:Object)
		pr:Person = Person(p)
		If(self.name > pr.name) Then
			Return 1
		Else 
			Return 0
		End If
	End Method	
	
	Method ToString:String()
	   	Local str:String
	   	str :+ "~nName:"+name+" Age:"+age
		Return str
	End Method
End Type

Type People Extends TList
	
	Method add(p:Person)
	  AddLast(p)
	End Method
	
	Method SortByName()
		Sort()
	End Method
	 
  	Method ToString:String()
		Local str:String
		For Local p:Person=EachIn Self
			str :+ p.ToString()
		Next
		Return str
  	End Method	
End Type

'===============================
'  main 
'===============================
p1:Person = Person.create("John",21)
p2:Person = Person.create("Adam",35)
p3:Person = Person.create("Michael",67)

p:People = New People

p.add(p1)
p.add(p2)
p.add(p3)
Print p.ToString()
p.SortByName()
Print p.ToString()



Hope this helps.

Levent