英文:
Sorting unsigned ints in Go
问题
我想对一个[]uint切片进行排序。这样做有意义吗?有没有什么关于uint类型使它们无法排序的问题?我注意到sort包中有IntSlice和Float64Slice,但没有uint的相关类型。
如果uint类型是可排序的,那么我是否可以直接复制sort.IntSlice中用于Len()、Less(i, j int)和Swap(i, j int)的实现呢?
英文:
I want to sort a slice of []uint. Does this make sense? Is there something I'm missing about uints that make them unsortable? I notice that the sort package has an IntSlice and Float64Slice, but nothing for uint.
If uints are sortable, then can I just copy the same implementation for Len(), Less(i, j int) and Swap(i, j int) that they've used for the sort.IntSlice?
答案1
得分: 4
关于1.7版本,你可以对任何实现了sort.Interface接口的切片进行排序,可以使用你的[]uint切片的别名类型,并使用sort.Sort(Interface)函数。在Go 1.8中,你还可以使用sort.Slice函数。可以在这里查看示例。
英文:
As for 1.7, you can sort any slice that implements sort.Interface for an alias type of your []uint slice and using sort.Sort(Interface) . In Go 1.8, you will have the option to use sort.Slice. See the example here
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论