我知道我们可以使用
sort.Sort(sort.Reverse(sort.IntSlice(example)))
排序数组.
但是我如何获得数组的索引?
例如
example := []int{1,25,3,5,4}
我想得到输出:1,4,2
为sort.IntSlice创建一个包装器,它会记住索引并在交换值时交换它们:type Slice struct { sort.IntSlice idx []int } func (s Slice) Swap(i,j int) { s.IntSlice.Swap(i,j) s.idx[i],s.idx[j] = s.idx[j],s.idx[i] }
type Slice struct { sort.Interface idx []int } func (s Slice) Swap(i,j int) { s.Interface.Swap(i,s.idx[i] }