fixed sort to handle multi-dim arrays