------------------------------------------------------------

// 满足 Interface 接口的类型可以被本包的函数进行排序。
type Interface interface {
// Len 方法返回集合中的元素个数
Len() int
// Less 方法报告索引 i 的元素是否比索引 j 的元素小
Less(i, j int) bool
// Swap 方法交换索引 i 和 j 的两个元素的位置
Swap(i, j int)
} // 对 data 进行排序(不保证相等元素的相对顺序不变)
// data 默认为升序,执行 Reverse 后为降序。
func Sort(data Interface) // 对 data 进行排序(保证相等元素的相对顺序不变)
// data 默认为升序,执行 Reverse 后为降序。
func Stable(data Interface) // 将 data 的排序动作更改为降序,Reverse 并不改变元素顺序,只改变排序行为。
// 更改操作不可逆,更改后的对象不可以再次 Reverse。
func Reverse(data Interface) Interface // 判断 data 是否已经排序
// 未执行 Reverse 的必须为升序,执行 Reverse 的必须为降序
func IsSorted(data Interface) bool ------------------------------ // 示例
func main() {
i := []int{3, 7, 1, 3, 6, 9, 4, 1, 8, 5, 2, 0}
a := sort.IntSlice(i)
fmt.Println(sort.IsSorted(a)) // false
sort.Sort(a)
fmt.Println(a) // [0 1 1 2 3 3 4 5 6 7 8 9]
fmt.Println(sort.IsSorted(a), "\n") // true b := sort.IntSlice{3}
fmt.Println(sort.IsSorted(b), "\n") // true // 更改排序行为
c := sort.Reverse(a)
fmt.Println(sort.IsSorted(c)) // false
fmt.Println(c) // &{[0 1 1 2 3 3 4 5 6 7 8 9]}
sort.Sort(c)
fmt.Println(c) // &{[9 8 7 6 5 4 3 3 2 1 1 0]}
fmt.Println(sort.IsSorted(c), "\n") // true // 再次更改排序行为
d := sort.Reverse(c)
fmt.Println(sort.IsSorted(d)) // false
sort.Sort(d)
fmt.Println(d) // &{0xc42000a3b0}
fmt.Println(sort.IsSorted(d)) // true
fmt.Println(d) // &{0xc42000a3b0}
} ------------------------------ // 对 a 进行升序排列
func Ints(a []int) // 判断 a 是否为升序排列
func IntsAreSorted(a []int) bool // 搜索 a 中值为 x 的索引,如果找不到,则返回最接近且大于 x 的值的索引,
// 可能是 len(a)。
func SearchInts(a []int, x int) int ------------------------------ // 示例
func main() {
a := []int{3, 9, 1, 6, 4, 2, 8, 2, 4, 5, 3, 0}
sort.Ints(a)
fmt.Println(a) // [0 1 2 2 3 3 4 4 5 6 8 9]
fmt.Println(sort.IntsAreSorted(a)) // true
i := sort.SearchInts(a, 7)
fmt.Println(a[i]) // 8
} ------------------------------ // 功能同上,类型不同
func Float64s(a []float64)
func Float64sAreSorted(a []float64) bool
func SearchFloat64s(a []float64, x float64) int // 功能同上,类型不同
func Strings(a []string)
func StringsAreSorted(a []string) bool
func SearchStrings(a []string, x string) int ------------------------------ // 实现了 sort.Interface 接口的 []int 类型
type IntSlice []int
func (p IntSlice) Len() int // 接口方法
func (p IntSlice) Less(i, j int) bool // 接口方法
func (p IntSlice) Swap(i, j int) // 接口方法 // 对 p 进行升序排列
func (p IntSlice) Sort() // 搜索 p 中值为 x 的索引,如果找不到,则返回最接近且大于 x 的值的索引,
// 可能是 len(a)。
func (p IntSlice) Search(x int) int ------------------------------ // 示例
func main() {
a := sort.IntSlice{3, 7, 1, 3, 6, 9, 4, 1, 8, 5, 2, 0}
a.Sort()
fmt.Println(a) // [0 1 1 2 3 3 4 5 6 7 8 9]
fmt.Println(sort.IsSorted(a)) // true
i := a.Search(6)
fmt.Println(i, a[i]) // 8 6
} ------------------------------ // 功能同上,类型不同
type Float64Slice []float64
func (p Float64Slice) Len() int
func (p Float64Slice) Less(i, j int) bool
func (p Float64Slice) Swap(i, j int)
func (p Float64Slice) Sort()
func (p Float64Slice) Search(x float64) int // 功能同上,类型不同
type StringSlice []string
func (p StringSlice) Len() int
func (p StringSlice) Less(i, j int) bool
func (p StringSlice) Swap(i, j int)
func (p StringSlice) Sort()
func (p StringSlice) Search(x string) int ------------------------------ // Search 采用二分法搜索,在小于 n 的索引中查找最小的满足 f(索引) 的值。返
// 回找到的索引,如果没有符合要求的索引,则返回 n。
func Search(n int, f func(int) bool) int ------------------------------ // 示例
func main() {
a := sort.StringSlice{"hello", "world", "golang", "sort", "nice"}
a.Sort() // 二分法必须先排序
// 获取首字母大于 n 的元素中最小的
i := sort.Search(len(a), func(i int) bool {
return len(a[i]) > 0 && a[i][0] > 'n'
})
// 显示找到的元素
fmt.Println(a[i]) // sort
} ------------------------------------------------------------

Golang学习 - sort 包的更多相关文章

  1. Golang学习 - reflect 包

    ------------------------------------------------------------ 在 reflect 包中,主要通过两个函数 TypeOf() 和 ValueO ...

  2. Golang学习 - io 包

    ------------------------------------------------------------ 先说一下接口,Go 语言中的接口很简单,在 Go 语言的 io 包中有这样一个 ...

  3. Golang学习 - unsafe 包

    ------------------------------------------------------------ 指针类型: *类型:普通指针,用于传递对象地址,不能进行指针运算. unsaf ...

  4. Golang学习 - errors 包

    ------------------------------------------------------------ Go 语言使用 error 类型来返回函数执行过程中遇到的错误,如果返回的 e ...

  5. Golang学习 - bytes 包

    ------------------------------------------------------------ 对于传入 []byte 的函数,都不会修改传入的参数,返回值要么是参数的副本, ...

  6. Golang学习 - bufio 包

    ------------------------------------------------------------ // bufio 包实现了带缓存的 I/O 操作 -------------- ...

  7. Golang学习 - strings 包

    ------------------------------------------------------------ strings 包与 bytes 包中的函数用法基本一样,不再赘述. 只对 R ...

  8. Golang学习 - builtin 包

    Go builtin包提供了go预先声明的函数.变量等的文档.这些函数变量等的实现其实并不是在builtin包里,只是为了方便文档组织. 这些内置的变量.函数.类型无需引入包即可使用. 默认提供的有: ...

  9. golang学习笔记--包导入及go 常用命令及参数

    包导入:包导入路劲即代码包在工作区的src目录下的相对路径. 同一个源码文件中导入的多个代码包的最后一个元素不能重复,否则引起编译错误,如果只导入不使用,同样会引起编译错误 若想导入最后一个元素名相同 ...

随机推荐

  1. NServiceBus教程-持久化

    NServiceBus的各种特性需要持久性.其中有超时.传奇和订阅存储. 四个持久化技术在NServiceBus在使用: RavenDB nHibernate 内存中 MSMQ 读到安装Raven D ...

  2. Labview中局部变量和全局变量

    局部变量的作用域是整个VI,它用于在单个VI中传输数据: 全局变量的作用域是整台计算机,它主要用于多个VI之间共享数据

  3. hdu 1176 免费馅饼(数塔类型)

    http://acm.hdu.edu.cn/showproblem.php?pid=1176 免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  4. AutoCAD.NET 不使用P/Invoke方式调用acad.exe或accore.dll中的接口(如acedCommand、acedPostCommand等)

    使用C#进行AutoCAD二次开发,有时候由于C#接口不够完善,或者低版本AutoCAD中的接口缺少,有些工作不能直接通过C#接口来实现,所以需要通过P/Invoke的方式调用AutoCAD的其他DL ...

  5. Python基础 列表

    ---***---传送门---***--- 文档解释 def append(self, p_object): """ L.append(object) -> Non ...

  6. No package identifier when getting name for resource number 0x00000000

    貌似在新版本的SDK下有时会出现标题的这个warning~  如下: 思前想后也不知道这个到底是说的哪里的问题,在逛谷歌的时候无意中发现有人说是因为xml中color的参数直接写成: android: ...

  7. ajax提交后台自动反序列化成对象

    下面是ajax提交的方法,主要用到了$form.serializeArray()将表单序列化成json对象,然后用each处理一下,代码如下: var $form = $('<form>& ...

  8. c++/java/c# 几种编程语言的指针、引用比较

    前一段时间,我在 cnblogs 别人的博客中,谈到: java 中的引用/指针,与 c++/C# 中的引用/指针不是一个概念. Java 引用,相当于 c++ 指针(fun3).Java 引用可以赋 ...

  9. 优化checkbox和radio,类似Bootstrap中的iCheck

    checkbox和radio浏览器默认的已经满足不了大众的审美需求,更不用说浏览器之间的差异化,取而代之,优化checkbox和radio的方法也随之诞生了. html结构:单选框为例,简单说明: 其 ...

  10. SQL SERVER 2005如何建立自动备份的维护计划

    SQL Server 2005中可以使用维护计划来为数据库自动备份,减少数据库管理员的工作负担.其使用方法如下: (1)启动[sql server Management Studio],在[对象资源管 ...