News
Forums
Code
Logs
Gallery
Specs
Search
Sort a list
Monkey Forums
/
Monkey Code
/
Sort a list
NoOdle
(Posted 2011)
[#1]
Quickly threw this together to sort a list with a simple example.
Import mojo 'stores highscore list Class Highscore Field scoreList : HighscoreList Method New() Self.scoreList = New HighscoreList End Method 'prints scores to the screen Method DrawScores() Local s : Score Local y : Int = 10 For s = Eachin scoreList DrawText s.name, 10, y DrawText s.value, 100, y, 0.5 y = y + 15 Next End Method End Class 'stores score and name Class Score Global scoreID : Int = 0 Field name : String Field value : Int Method New() scoreID = scoreID + 1 Self.name = "Player" + String( scoreID ) Self.value = Rnd( 100 ) End Method End Class 'extends the list Class HighscoreList Extends List <Score> 'compares score value Method Compare( a : Score, b : Score ) If a.value > b.value Then Return 1 If b.value < a.value Then Return 0 Return -1 End Method End Class Class MyApp Extends App Field scoreTable : Highscore Method OnCreate() SetUpdateRate 60 'creates score contains scoreTable = New Highscore() 'creates some random scores and add them to the list For Local i : Int = 0 To 20 Local s : Score = New Score() scoreTable.scoreList.AddLast( s ) Next 'sorts the list scoreTable.scoreList.Sort( False ) End Method Method OnUpdate() End Method Method OnRender() Cls 0, 0, 0 'prints the sorted list scoreTable.DrawScores() End Method End Class Function Main() New MyApp() End Function