loop through (sort by) for each by field value ?

Monkey Forums/Monkey Programming/loop through (sort by) for each by field value ?

GC-Martijn(Posted 2015) [#1]
I'm building a z-index for drawing everything on the write place,
and want to be able to adjust the z-index later.

But where I'm stuck is how to loop through all the items by a field integer ?

First I did create a IntMap to loop through it by key, but I wan't able to change the key value at a later point, and it din't feel right.

So now I use the Stack<Sprite> with a field zindex:Int
But how to loop through the items sorted by zindex ?

Class Sprite
	Field zindex:Int
End


Field spriteCollection:Stack<Sprite> = New Stack<Sprite>()
	spriteCollection.Push(_sprite) ' zindex 3
	spriteCollection.Push(_sprite) ' zindex 1
	spriteCollection.Push(_sprite) ' zindex 4
        .... more

Onrender
For Local sprite:Sprite = Eachin spriteCollection
	' loop through it in the zindex field order 1,3,4
Next

Onupdate
Here I change the zorder if I need/want to (already have that)


Now I'm writing this, I could maybe create a temp sorted list ;)
For Local sprite:Sprite = Eachin spriteCollection
	' loop through it in the zindex field order 1,3,4
 tempmap[sprite.zindex] = sprite
Next
and now loop the tempmap 



GC-Martijn(Posted 2015) [#2]
I'm building a z-index for drawing everything on the write place,
and want to be able to adjust the z-index later.

But where I'm stuck is how to loop through all the items by a field integer ?

First I did create a IntMap to loop through it by key, but I wan't able to change the key value at a later point, and it din't feel right.

So now I use the Stack<Sprite> with a field zindex:Int
But how to loop through the items sorted by zindex ?

Class Sprite
	Field zindex:Int
End


Field spriteCollection:Stack<Sprite> = New Stack<Sprite>()
	spriteCollection.Push(_sprite) ' zindex 3
	spriteCollection.Push(_sprite) ' zindex 1
	spriteCollection.Push(_sprite) ' zindex 4
        .... more

Onrender
For Local sprite:Sprite = Eachin spriteCollection
	' loop through it in the zindex field order 1,3,4
Next

Onupdate
Here I change the zorder if I need/want to (already have that)


Now I'm writing this, I could maybe create a temp sorted list ;)
For Local sprite:Sprite = Eachin spriteCollection
	' loop through it in the zindex field order 1,3,4
 tempmap[sprite.zindex] = sprite
Next
and now loop the tempmap 



muddy_shoes(Posted 2015) [#3]
You may like to read these old threads:

http://www.monkey-x.com/Community/posts.php?topic=1527
http://www.monkey-x.com/Community/posts.php?topic=1257

One note: There's nothing stopping you updating the z-position of an object if you use IntMaps. You just remove the item using the old key and then add it again with the new one. The bigger issue with an IntMap is that you'll need to add handling for multiple objects at the same depth yourself.