Aggregate tensors in a specific order

I have 10 images that I want to merge into a single image.
These images are RGBA tensors and thus have the shape: [N C H W] = [10, 4, 64, 64]

The way images are being merged depends on their order. Later images are being “added” on top of earlier ones; replacing some pixels in the final output image. I want the images to be ordered according to a second tensor z_values of shape [N].

I could try to sort the images given indices from torch.argsort(z_values) but that would not be differentiable. Any ideas on how I might achieve this?