well, I recreated your project... and I could not get it to fail. It worked just the way you wanted... and I tried several variations.
I added Drag and Drop to the blocks so I could move them around to see how they were stacked.
I also added an animation - grey for the bottom layer, white for the top, to help see which blocks are which.
And I added a text box at the top of the screen to display the block coordinates when you click on it or move it.
My first thought when I started was that your sort routine was not working the way you expected. You sort on arrayZ, but the 25 blocks that make up the upper layer could display incorrectly (like in your picture) if you don't also sort them by arrayY - because the blocks with a lower arrayY need to be behind the ones with a higher arrayY. But, like I said, it worked fine for me when I tried it.
I am using the latest version of C2, so it could be an issue with an older version - where the ambiguity of just sorting 25 blocks with the same arrayZ gives a random order or something like that...
anyway, try my version and see what happens. you can get it at:
http://www.rieperts.com/games/forum/Blocks.capx
In my version, if you press A it creates another set of blocks using loops rather than the array. But that way also worked as expected.