Move Object in TList

BlitzMax Forums/BlitzMax Programming/Move Object in TList

I have a TList and I need to move 1 item up or down the list 1 place. My brain is failing me and I can't seem to even hack something ugly together that works. Any suggestions on how to accomplish this?

Have you researched the sort commands yet?

I was looking at SortList as an option but the list order is what I'm using for my item order, so to move something up or down, as I understand it, I would have to be able to compare two items to determine which goes above another, but the way I currently determine what is above something else is just it's order in the list so I would need a second way of tracking an item essentially to be able to shift it.


SuperStrict

Framework brl.standardio
Import brl.linkedlist

Local mylist:TList = New(TList)

Local myfirstobject:String = "Hello"
Local mysecondobject:String = "World!"
mylist.AddLast(myfirstobject)
mylist.AddLast(mysecondobject)
mylist.AddLast(":P")

Print "Moved: " + MoveObjectUp(mylist, mysecondobject)
Print "Moved: " + MoveObjectDown(mylist, myfirstobject)

For Local value:String = EachIn mylist
	
	Print "~q" + value + "~q"
	
Next
End

Function MoveObjectUp:Int(list:TList, obj:Object)
  Local link:TLink, prev:TLink
	
	If list.Count() = 0 Then Return False
	
	link = list.FindLink(obj)
	If link = Null Then Return False
	
	prev = link.PrevLink()
	If prev = Null Then Return False ' Already at the top of the list
	
	link = Null
	list.Remove(obj)
	list.InsertBeforeLink(obj, prev)
	
	Return True
	
End Function

Function MoveObjectDown:Int(list:TList, obj:Object)
  Local link:TLink, nextlink:TLink
	
	If list.Count() = 0 Then Return False
	
	link = list.FindLink(obj)
	If link = Null Then Return False
	
	nextlink = link.NextLink()
	If nextlink = Null Then Return False ' Already at the bottom of the list
	
	link = Null
	list.Remove(obj)
	list.InsertAfterLink(obj, nextlink)
	
	Return True
	
End Function



oooo that looks like exactly what I need thank you!