ensure sorted k-nearest indices