1 回答

TA贡献1839条经验 获得超15个赞
如果索引 i 处的值小于索引 i 处的值,您需要提供的func应该返回 true。sort.Slice
因此,您应该将==
and!=
替换为<
,或 with>=
进行反向排序。
Go 没有隐式类型大小写,因此在您的less
func 中,您必须使用类型开关之类的东西检查每个字段的类型,并根据您找到的类型处理比较。
例如:
sort.Slice(hives, func(i, j int) bool {
aInt := hives[i][gpi.SortBy]
bInt := hives[j][gpi.SortBy]
switch a := aInt(type) {
case int:
if b, ok := bInt.(int); ok {
return a < b
}
panic("can't compare dissimilar types")
case string:
if b, ok := bInt.(string); ok {
return a < b
}
panic("can't compare dissimilar types")
default:
panic("unknown type")
}
})
- 1 回答
- 0 关注
- 117 浏览
添加回答
举报