Custom Compare() method causes problems...

BlitzMax Forums/BlitzMax Programming/Custom Compare() method causes problems...

Hi,
Below you find a very familiar piece of code for managing all elements in your game.
I was experimenting with the Compare() method to change the order of the list. In that way I'm able to draw elements in the correct order on the screen if I give each element a Z-value (higher values are on top of lower values). This works great (you can try) but if you have more than one element with the same Z-value (which should be possible) you can not remove that element from the list with the provided functions: Remove(), RemoveLink() or ListRemove() because these functions use the Compare() method to find the right link in the list.

I would like to keep the z-ordeing thing AND be able to manage the list properly.
Any ideas how to solve this?
Type TElement
	Global glElements:TList

	Field piZ% = 0			'used for layering the sprites (higher Z-values are on top of lower Z-values)

	Method New() 
		If Not glElements Then glElements = CreateList() 
		glElements.AddLast(Self) 
	End Method
	
	Method Kill() 
'none of these functions work because they all use Compare() to find the proper link :(
'		RemoveLink(ListFindLink(glElements, Self)) 
'		ListRemove(glElements, Self)
		glElements.Remove(Self) 
	End Method
		
   Function UpdateAll() 
      For Local e:TElement = EachIn glElements
         e.update() 
      Next
   End Function
	
'	You must actively call this function in order to get all sprites sorted by Z-value (performance issue)
'	optional argment 'bAscending' can be used to sort the sprites reversed (highest Z-value first)  
	Function SortZ(bAscending% = True) 
		glElements.Sort(bAscending) 
	End Function
		
'	This method is used for sorting the glSprites list in Z-order (ascending)
	Method Compare%(OtherObj:Object) 
		Local e:TElement = TElement(OtherObj) 
		If Not e Return Null
		Return piZ - e.piZ
	End Method


'	Abstract Methods
	Method Update() Abstract
	Method Draw() Abstract

'	----------------
End Type