sort a tlist
BlitzMax Forums/BlitzMax Programming/sort a tlist
| ||
| k, i have a tlist called displaylist eg: For Local d:displayimage = EachIn displaylist next each displayimage has an x and y, i need to insert the current displayimage before the last if the x or y are lower than the prev displayimage`s values. ive had a search around but it seems overly complicated for what was such a simple thing todo in b3d etc. any ideas? |
| ||
| Won't you need InsertAfterLink or InsertBeforeLink? |
| ||
| dont seem to work? |
| ||
Local tmpimage:displayimage Local tmpx:Int,tmpy:Int For Local d:displayimage = EachIn displaylist If tmpimage If d.x < tmpimage.x Or d.y < tmpimage.y 'insert d before tmpimage here,... but how? EndIf EndIf tmpimage = d tmpx = d.x tmpy = d.y Next |
| ||
| This should help. Links |
| ||
| -below post- |
| ||
seems a bit... slow?, i basically need to sort the displayimages based on the y location, so they draw in order,Local tmpimage:displayimage Local tmpx:Int,tmpy:Int Local zupdate:Byte For Local d:displayimage = EachIn displaylist zupdate = False If tmpimage <> Null If d.y < tmpimage.y zupdate = True 'ElseIf d.x < tmpimage.x ' zupdate = True EndIf If zupdate displaylist.remove(tmpimage) displaylist.insertafterlink(tmpimage,displaylist.findlink(d)) EndIf EndIf tmpimage = d tmpx = d.x tmpy = d.y Next |
| ||
Type DisplayImage
Field X:Int
Field Y:Int
Global List:TList = New TList
Method Compare( other:Object )
Local i:DisplayImage = DisplayImage( other )
If X < i.X Or Y < i.Y Then Return -1
Return 0
End Method
Method New( )
List.AddLast( Self )
End Method
End Type
Something like this? |
| ||
Something like this?
Strict
Type test
Field link:TLink
Field a:Int
End Type
Local list:TList = New TList
For Local i:Int=1 To 8
Local t:test = New test
t.a = i
t.link = list.AddLast(t)
Next
Local j:test = New test
j.a = 5
Local done:Int
For Local t:test=EachIn list
If done=0
If t.a > j.a
list.insertbeforelink(j,t.link)
done=1
EndIf
EndIf
Next
For Local t:test=EachIn list
Print t.a
Print
Next
Not sure of a better way to exit a for/eachin loop. Exit seems to exit without doing the insert. |
| ||
| its sorted now, just override the compare method and call sortlist, worked out good. |
| ||
| Can you show the code? |
| ||
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 |
| ||
Method Compare(p:Object) pr:Person = Person(p) If(self.name > pr.name) Then Return 1 Else Return 0 End If End Method Are you sure that always works? Returning zero means that the two are identical, but what if the string you're comparing to is "less than"? Would it still do the right thing anyway? This is my attempt to explain it: Type MySorter Field CompareValue:Int Method Compare(o:Object) MS:MySorter = MySorter(o) Return (Self.CompareValue - MS.CompareValue) ' Positive number: The compared object is < this object. ' Zero: The compared object is = this object. ' Negative number: The compared object is > this object. End Method Function PrintList(MSL:TList) PrintStr$ = "" For MS:MySorter = EachIn MSL PrintStr$ :+ MS.CompareValue + ", " Next Print PrintStr End Function Function Create:MySorter(CV:Int) MS:MySorter = New MySorter MS.CompareValue = CV Return MS End Function End Type Local MSList:TList = New TList For i = 0 To 24 MSList.AddLast( MySorter.Create(Rand(50)) ) Next Print "Unsorted:" MySorter.PrintList(MSList) MSList.Sort() Print "Sorted:" MySorter.PrintList(MSList) |