gpt4 book ai didi

sorting - 如何按多个值对 go slice 进行排序?

转载 作者:数据小太阳 更新时间:2023-10-29 03:12:58 26 4
gpt4 key购买 nike

type Item struct {
Y int
X int
otherProp int
}

我有一片像上面那样的结构。如何像 SQL 中的 ORDER BY X,Y 一样先按 X 值然后按 Y 值对 slice item []Item 进行排序?

我看到您可以使用 sort.Slice() 自 go 1.8 以来,但是有没有一种简单的方法可以解决这个问题而无需多次循环 slice ?

最佳答案

[...] is there a simple way to solve this without looping over the slice several times?

没有。基于比较的排序基本上总是涉及至少循环 slice 一次再加上更多。但不要担心:sort.Slice 不会做太多工作。

你的问题是什么?

关于sorting - 如何按多个值对 go slice 进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46199397/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com