SortList Compare function?

BlitzMax Forums/BlitzMax Beginners Area/SortList Compare function?

ima747(Posted 2008) [#1]
I've read up a bit on the sortlist command and I've setup a compare function but I'm getting really weird compile errors that I don't understand

Type TPiece
	Field PieceDepth:Int
	
	Method Compare(Other:Object)
		If PieceDepth > TPiece(Other).PieceDepth Then Return 1 Else Return -1
	End Method


With superstrict on I get "expression of type byte cannot be invoked". With suprstrict off I get "Identifier TPiece not found". Either way it won't compile. I've pretty much copied this straight out of all the tutorials and I've never had problems declaring an object as a type before so I'm more than a bit confused...


degac(Posted 2008) [#2]
SuperStrict

Type TPiece
	Field PieceDepth:Int
	Global list:TList=New TList
	
	Method Compare:Int(Other:Object)
		If PieceDepth > TPiece(Other).PieceDepth Then Return 1 Else Return -1
	End Method
	
	Method Add:Int()
		Self.pieceDepth:Int=MilliSecs()
		tpiece.list.addlast Self
	End Method
	
	Function Show()
		For Local s:tpiece=EachIn tpiece.list
			Print s.tostring()+" "+s.pieceDepth
		Next
	End Function
	
End Type


Local a1:tpiece=New tpiece
Delay 10
Local a2:tpiece=New tpiece


a1.add()
a2.add()
tpiece.show()
tpiece.list.sort()
tpiece.show()

No problems.


ima747(Posted 2008) [#3]
That compiles fine, but I still can't get the compare function to work in my type in my actual game, same error...

I have a Type of TPuzzle with Field Pieces:TList which is what I'm trying to sort. The Pieces is a TList of TPiece objectes, if that makes any difference but as far as I can understand it shouldn't. I should me able to do
MyPuzzle.Pieces.Sort() and it should use the compare function of the TPiece type to do the sort ordering. But that function won't let TPiece(Other) work. It just says Identifier TPiece not found... which doesn't make any sense to me because it's IN the TPiece type...


grable(Posted 2008) [#4]
It might not be sorting correctly, as you return -1 even if they are equal.
On an other note, did you notice the return type of Compare is Int?


ima747(Posted 2008) [#5]
Yes, and it dies before the sort, it's

TPiece(Other)


that causes the compile error.


Brucey(Posted 2008) [#6]
You may have a method/function called TPiece, or perhaps a global variable, or field... It can get confused if you mix them up like that.


ima747(Posted 2008) [#7]
Aha! my TPiece type somehow got a TPiece field in it... lord knows how I typo'd that one up... thanks!


Brucey(Posted 2008) [#8]
Yay ;-)