Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
I had another idea, very similar, but I think the code would be clearer, don't know if it would be more or less efficient though. The idea would be to create one texture for each level and then render directly to that texture acording to the object Z level, then just render the textures to the screen in order. This way, you don't need to store a lot of data to render later.