generalised Sort()