Anyone got some good Example of how to use complex sorting?

Started by Hardcoal, December 24, 2024, 10:29:15

Previous topic - Next topic

Hardcoal

When I want to sort a Type for example

global TeamsList:tlist=createlist()

Type Team_Type
     field Odds
     field TeamsName$
end Type

global Team:Team_Type=New Team_Type
team.Odds=34
listaddLast(Teamslist,Team)


[font=Verdana, Arial, Helvetica, sans-serif]global Team2:Team_Type=New Team_Type
team.Odds=56
listaddLast(Teamslist,Team2)[/font]

I want to sort it by Odds Value Ascending..

there is the Teamslist.Sort option

Now can anyone show me an example using the Function Option?

Sort( ascending=True,compareFunc( o1:Object,o2:Object )=CompareObjects )

Thanks
Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]

Henri

Hi,

here is an example how to sort by field value.

'Create some new objects with random id
For Local i:Int = 1 To 10
TBase.Create( Rand(1, 50) )
Next

'Sort in ascending order based on id
SortList(TBase.list, True, SortByID)

'See the results
For Local b:TBase = EachIn TBase.list
Print b.id
Next

Type TBase
Field id:Int

Global list:TList = New TList

Function Create(new_id:Int)
Local b:TBase = New TBase
b.id = new_id
list.addlast(b)
EndFunction
EndType

Function SortByID:Int(o1:Object, o2:Object)

Local b1:TBase = TBase(o1)
Local b2:TBase = TBase(o2)

If b1.id > b2.id Then
Return 1
ElseIf b1.id < b2.id
Return -1
Else
Return 0
EndIf
EndFunction

E: tabs are there, but forum software doesn't display them.

-Henri
- Got 01100011 problems, but the bit ain't 00000001

Hardcoal

Things I've done:   https://itch.io/profile/hardcoal  [I keep improving them btw]