在Go中对无符号整数进行排序(Sorting unsigned ints in Go)

编程入门 行业动态 更新时间:2024-10-25 13:30:58
在Go中对无符号整数进行排序(Sorting unsigned ints in Go)

我想整理一下[]uint 。 这有意义吗? 是否有一些我对uint s缺失的东西让它们变得难以辨认? 我注意到sort包有一个IntSlice和Float64Slice ,但没有任何uint 。

如果uint 可排序的,那么我可以只复制他们用于sort.IntSlice Len() , Less(i, j int)和Swap(i, j int)的sort.IntSlice吗?

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.7,您可以对实现sort.Interface的任何切片进行排序,以获得[] uint切片的别名类型并使用sort.Sort(接口) 。 在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

更多推荐

本文发布于:2023-07-04 11:09:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1023614.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   中对   符号   ints   Sorting

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!