Home
Products
Community
Manuals
Contact
Login or Signup

BlitzMax Docs -> 2D - Category -> Data -> SortList

SortList( list:TList,ascending=True,compareFunc( o1:Object,o2:Object ) )

Returns:

Nothing.

Description:

Sort a list


Comments

Plash(Posted 1+ years ago)
Also note that you can call 'mylist.Sort(ascending, comparefunc)' to the same effect.


degac(Posted 1+ years ago)
Basic example of SortList
'SortList
'Bmax 1.34 - 05 sept 2009
SuperStrict

Local list:TList=New TList

ListAddLast list,"One"
ListAddLast list,"Two"
ListAddLast list,"Three"
ListAddLast list,"Four"

Print "The list contains the following items..."
For Local it:String=EachIn list
	Print it
Next

'now sort the list in alphabetic order

SortList(list)
Print

Print "The list contains the now following items..."
For Local it:String=EachIn list
	Print it
Next

Example 2 : user type based list
'SortList
'Bmax 1.34 - 05 sept 2009
SuperStrict

Local list:TList=New TList

Type usertype
	Field name:String
	Field value:Int

	Method Create:usertype(_na$="",_val:Int=0)
		name=_na
		value=_val
		Return Self
	End Method
End Type

ListAddLast list,New usertype.Create("Jim",10)
ListAddLast list,New usertype.Create("Tom",25)
ListAddLast list,New usertype.Create("Bit",2)

Print "List contains "+CountList(list)+" items"

For Local it:usertype=EachIn list
	Print it.name+" "+it.value
Next

'sorting the list
SortList(list)

Print
For Local it:usertype=EachIn list
	Print it.name+" "+it.value
Next
'there is no effect on a USER TYPE based list...
'you need to create you own COMPARE method (used by SortList)

Example 3 - User type with its own Compare Method
'SortList
'Bmax 1.34 - 05 sept 2009
SuperStrict

Local list:TList=New TList

Type usertype
	Field name:String
	Field value:Int

	Method Create:usertype(_na$="",_val:Int=0)
		name=_na
		value=_val
		Return Self
	End Method
		
	Method Compare:Int(Other:Object)
		If other=Null Return 0
		If value=UserType(other).value Return 0
		If value<UserType(Other).value Then Return 1 Else Return -1
	End Method
	
End Type



ListAddLast list,New usertype.Create("Jim",10)
ListAddLast list,New usertype.Create("Tom",25)
ListAddLast list,New usertype.Create("Bit",2)

Print "List contains "+CountList(list)+" items"

For Local it:usertype=EachIn list
	Print it.name+" "+it.value
Next

'sorting the list
'in this case using as key for the order the field VALUE
'see the COMPARE method I've added
SortList(list)

Print
For Local it:usertype=EachIn list
	Print it.name+" "+it.value
Next

Example 4 - Sorting with multiple fields
'SortList
'Bmax 1.34 - 05 sept 2009
SuperStrict

Local list:TList=New TList

Type usertype
	
	Global what_order:Int

	Field name:String
	Field value:Int

	Method Create:usertype(_na$="",_val:Int=0)
		name=_na
		value=_val
		Return Self
	End Method
		
	Method Compare:Int(Other:Object)
		If other=Null Return 0
		
		If what_order=0 '	this means (for use) 'order bades on VALUE field
			If value=UserType(other).value Return 0
			If value<UserType(Other).value Then Return 1 Else Return -1
		Else	
                        'order based on NAME field
			If name=UserType(other).name Return 0
			If name>UserType(other).name Then Return 1 Else Return -1		
		End If
	End Method
	
End Type

ListAddLast list,New usertype.Create("Jim",10)
ListAddLast list,New usertype.Create("Tom",25)
ListAddLast list,New usertype.Create("Bit",2)

Print "List contains "+CountList(list)+" items"

For Local it:usertype=EachIn list
	Print it.name+" "+it.value
Next

'sorting the list
'see the COMPARE method I've added
'now we can choose what order-field to use 

usertype.what_order=1 ' order by NAME (in our example)

SortList(list)
Print
For Local it:usertype=EachIn list
	Print it.name+" "+it.value
Next



TAS(Posted 7 months ago)
Great examples degac!


BlitzMax Manual Forum

Blitz3D Equivalent Command