Fixed sort to not mutate the original array