Remark 1
instead of going over the entire list and each time search for a Z it would be faster to sort the entire list by Z and then just display it in the right Z order. But sorting in my experience is hard for a beginner
Remark 2
I am not sure this will prevent that.
If not:
Instead when you click on a field and there are e.g. three garden objects underneath, you have to check which one of those has the highest Z and delete only this one.
Which is in itself a bit tricky. Rough description: You need to store the highest Z as maxZ
and compare against this and also store the index of the garden object with the highest Z (maxZ_Index
) and after the loop delete the garden object with this index.