我想为结构的 2D 切片创建一致的顺序,我正在从地图创建 2D 切片,因此顺序总是不同的。
我的结构看起来像
// Hit contains the data for a hit.
type Hit struct {
Key string `json:"key"`
Data []Field `json:"data"`
}
// Hits stores a list of hits.
type Hits [][]Hit
我想为我的类型内容提供一致的顺序Hits。
我努力了:
func (c Hits) Len() int { return len(c) }
func (c Hits) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
func (c Hits) Less(i, j int) bool { return strings.Compare(c[i][0].Key, c[j][0].Key) == -1 }
但结果似乎仍然以随机顺序返回。
我正在考虑可能对切片中的每个项目进行哈希处理,但认为可能有一个更简单的选择
繁星淼淼
相关分类