Sorting a lift by a field in ascending order
BlitzMax Forums/BlitzMax Beginners Area/Sorting a lift by a field in ascending order
| ||
Hi ! Can someone exmplain me how to sort the number in ascending order on this exemple : Global List:TList = CreateList() Type mytype Field value:Int Function create:mytype(val:Int) Local t:mytype = New mytype t.value = val Return t End Function End Type Global object1:mytype Global object2:mytype Global object3:mytype object1 = mytype.Create(12) object2 = mytype.Create(6) object3 = mytype.Create(28) List.AddLast object1 List.AddLast object2 List.AddLast object3 SortList(list,True) For i:mytype = EachIn list Print i.value Next |
| ||
I whish we can edit title... a list not a lift |
| ||
Global List:TList = CreateList() Type mytype Field value:Int Function create:mytype(val:Int) Local t:mytype = New mytype t.value = val ListAddLast(List,t) Return t End Function Method Compare:Int(This:Object) Return Value - mytype(This).value EndMethod End Type mytype.Create(12) mytype.Create(6) mytype.Create(28) List.Sort(True) For i:mytype = EachIn list Print i.value Next |
| ||
oh, I see you asked for an exmplanation :), so here goes... To be able to sort a custom type you need to provide a compare method, this takes an object as its param, you then need to work out if this objects value is less/equal or greater than its instance value, you return <0 if its less, 0 if its equal or >0 if its greater. Hope that helps. |
| ||
Many thanks Papa Lazarou :) You helped me very much, the TList TLink and TLinkEnum is NOT really very well documented :( |