On a GPU usually what you do is just draw everything in any order, and the GPU will figure out how to composite them (which goes on top, and which goes behind) based on the depth buffer.
However, when using alpha transparency, objects must be drawn in the correct order. This means that sorting has to happen on the CPU, and be based on object position rather than happen per-pixel.
Unfortunately, this can result in so called “z-fighting” where two objects take turns being in front of each other based on rounding errors or as the camera moves around. There is no way of solving this with reasonable performance.
However, there are ways that you can “hack” it to affect the sorting algorithm based on your own logic of which should go in front and which should go behind. Here’s another discussion on this topic: