golang sort包 排序
[]float64:
ls := sort.Float64Slice{
1.1,
4.4,
5.5,
3.3,
2.2,
}
fmt.Println(ls) //[1.1 4.4 5.5 3.3 2.2]
sort.Float64s(ls)
fmt.Println(ls) //[1.1 2.2 3.3 4.4 5.5]
[]int:
ls := sort.IntSlice{
,
,
,
,
,
}
fmt.Println(ls) //[1 4 5 3 2]
sort.Ints(ls)
fmt.Println(ls) //[1 2 3 4 5]
string:
//字符串排序,现比较高位,相同的再比较低位
ls := sort.StringSlice{
"",
"",
"",
"",
"",
}
fmt.Println(ls) //[100 42 41 3 2]
sort.Strings(ls)
fmt.Println(ls) //[100 2 3 41 42] //字符串排序,现比较高位,相同的再比较低位
ls := sort.StringSlice{
"d",
"ac",
"c",
"ab",
"e",
}
fmt.Println(ls) //[d ac c ab e]
sort.Strings(ls)
fmt.Println(ls) //[ab ac c d e] //汉字排序,依次比较byte大小
ls := sort.StringSlice{
"啊",
"博",
"次",
"得",
"饿",
"周",
}
fmt.Println(ls) //[啊 博 次 得 饿 周]
sort.Strings(ls)
fmt.Println(ls) //[博 周 啊 得 次 饿] for _, v := range ls{
fmt.Println(v, []byte(v))
} //博 [229 141 154]
//周 [229 145 168]
//啊 [229 149 138]
//得 [229 190 151]
//次 [230 172 161]
//饿 [233 165 191]
复杂结构:
1. [][]int :
type testSlice [][]int func (l testSlice) Len() int { return len(l) }
func (l testSlice) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l testSlice) Less(i, j int) bool { return l[i][] < l[j][] } func main() {
ls := testSlice{
{,},
{,},
{,},
} fmt.Println(ls) //[[1 4] [9 3] [7 5]]
sort.Sort(ls)
fmt.Println(ls) //[[9 3] [1 4] [7 5]]
}
2. []map[string]int [{"k":0},{"k1":1},{"k2":2] :
type testSlice []map[string]float64 func (l testSlice) Len() int { return len(l) }
func (l testSlice) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l testSlice) Less(i, j int) bool { return l[i]["a"] < l[j]["a"] } //按照"a"对应的值排序 func main() {
ls := testSlice{
{"a":, "b":},
{"a":, "b":},
{"a":, "b":},
} fmt.Println(ls) //[map[a:4 b:12] map[a:3 b:11] map[a:5 b:10]]
sort.Sort(ls)
fmt.Println(ls) //[map[a:3 b:11] map[a:4 b:12] map[a:5 b:10]]
}
3. []struct :
type People struct {
Name string `json:"name"`
Age int `json:"age"`
} type testSlice []People func (l testSlice) Len() int { return len(l) }
func (l testSlice) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l testSlice) Less(i, j int) bool { return l[i].Age < l[j].Age } func main() {
ls := testSlice{
{Name:"n1", Age:},
{Name:"n2", Age:},
{Name:"n3", Age:},
} fmt.Println(ls) //[{n1 12} {n2 11} {n3 10}]
sort.Sort(ls)
fmt.Println(ls) //[{n3 10} {n2 11} {n1 12}]
}
4. 复杂的时候,按float64类型排序:
type People struct {
Name string `json:"name"`
Age float64 `json:"age"`
}
func isNaN(f float64) bool {
return f != f
}
type testSlice []People func (l testSlice) Len() int { return len(l) }
func (l testSlice) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l testSlice) Less(i, j int) bool { return l[i].Age < l[j].Age || isNaN(l[i].Age) && !isNaN(l[j].Age)} func main() {
ls := testSlice{
{Name:"n1", Age:12.12},
{Name:"n2", Age:11.11},
{Name:"n3", Age:10.10},
} fmt.Println(ls) //[{n1 12.12} {n2 11.11} {n3 10.1}]
sort.Sort(ls)
fmt.Println(ls) //[{n3 10.1} {n2 11.11} {n1 12.12}]
}
golang sort包 排序的更多相关文章
- golang sort包使用
https://studygolang.com/static/pkgdoc/pkg/sort.htm#StringSlice.Search package main import ( "fm ...
- Golang学习 - sort 包
------------------------------------------------------------ // 满足 Interface 接口的类型可以被本包的函数进行排序. type ...
- go 中 sort 如何排序,源码解读
sort 包源码解读 前言 如何使用 基本数据类型切片的排序 自定义 Less 排序比较器 自定义数据结构的排序 分析下源码 不稳定排序 稳定排序 查找 Interface 总结 参考 sort 包源 ...
- 数据结构和算法(Golang实现)(25)排序算法-快速排序
快速排序 快速排序是一种分治策略的排序算法,是由英国计算机科学家Tony Hoare发明的, 该算法被发布在1961年的Communications of the ACM 国际计算机学会月刊. 注:A ...
- 数据结构和算法(Golang实现)(24)排序算法-优先队列及堆排序
优先队列及堆排序 堆排序(Heap Sort)由威尔士-加拿大计算机科学家J. W. J. Williams在1964年发明,它利用了二叉堆(A binary heap)的性质实现了排序,并证明了二叉 ...
- redis 的使用 (sort set排序集合类型操作)
sort set排序集合类型 释义: sort set 是 string 类型的集合 sort set 的每个元素 都会关联一个 权 通过 权值 可以有序的获取集合中的元素 应用场合: 获取热门帖子( ...
- counting sort 计数排序
//counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorit ...
- Golang fmt包使用小技巧
h1 { margin-top: 0.6cm; margin-bottom: 0.58cm; direction: ltr; color: #000000; line-height: 200%; te ...
- Collections.sort自定义排序的使用方法
Collections.sort自定义排序的使用方法 总结:Collections可以对List进行排序:如果想对Map进行排序,可以将Map转化成List,进行排序: public static v ...
随机推荐
- webStorm Ctrl+s 自动格式化 然后 保存 用宏命令
使用WebStorm的Macros宏指令,实现保存的同时格式化代码,并跳至行尾 https://blog.csdn.net/gyz718/article/details/70556188
- Airbnb:别抵制我,宝宝要过 10 岁生日
今日导读 喜欢旅游的你,一定听说或使用过 airbnb(爱彼迎),在出发前打开它,总是能通过它开始一段奇妙的旅行.可是最近,就在这个打着“共享家”概念的服务型网站正迎来十周年之际,它却遭到了很多国家的 ...
- 美国司法部解禁guns打印技术
今日导读 你知道什么是 3D 打印吗?简单的说,只要有一张设计蓝图和适当的材料,就可以快速打印出实体物件.而最近据外媒报道,从今年 8 月 1 日起,在美国,拥有或公布枪支 3D 打印蓝图的行为都将属 ...
- 8 Java 归并排序(MergerSort)
图片素材与文字描述来自:尚硅谷-韩顺平数据结构与算法. 1.基本思想 归并排序是利用归并的思想实现的排序方法,该算法采用经典的分治(divide-and-conquer)策略(分治法将问题分(divi ...
- tomcat假死现象 - 二
1 编写背景 最近服务器发现tomcat的应用会偶尔出现无法访问的情况.经过一段时间的观察最近又发现有台tomcat的应用出现了无法访问情况.简单描述下该台tomcat当时具体的表现:客户端请求没有响 ...
- CSS - position属性小结
Relative: 属于文档流,针对自身进行偏移: Absolute: 脱离文档流,针对最近的定位元素进行偏移,如果没有,则针对根元素,即body标签尽心偏移: Fixed: 和absolute基本一 ...
- 674. Longest Continuous Increasing Subsequence@python
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...
- Bzoj 4720 换教室 (期望DP)
刚发现Bzoj有Noip的题目,只会换教室这道题..... Bzoj 题面:Bzoj 4720 Luogu题目:P1850 换教室 大概是期望DPNoip极其友好的一道题目,DP不怎么会的我想到了,大 ...
- 009 CSS选择器
CSS选择器 一.基础选择器 1.通配选择器 * { border: solid; } 匹配文档中所有标签:通常指html.body及body中所有显示类的标签 2.标签选择器 div { backg ...
- getComputedStyle与currentStyle获取元素当前的css样式
CSS的样式分为三类: 内嵌样式:是写在标签里面的,内嵌样式只对所在的标签有效内部样式:是写在HTML里面的,内部样式只对所在的网页有效外部样式表:如果很多网页需要用到同样的样式,将样式写在一个以.c ...