[]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包 排序的更多相关文章

  1. golang sort包使用

    https://studygolang.com/static/pkgdoc/pkg/sort.htm#StringSlice.Search package main import ( "fm ...

  2. Golang学习 - sort 包

    ------------------------------------------------------------ // 满足 Interface 接口的类型可以被本包的函数进行排序. type ...

  3. go 中 sort 如何排序,源码解读

    sort 包源码解读 前言 如何使用 基本数据类型切片的排序 自定义 Less 排序比较器 自定义数据结构的排序 分析下源码 不稳定排序 稳定排序 查找 Interface 总结 参考 sort 包源 ...

  4. 数据结构和算法(Golang实现)(25)排序算法-快速排序

    快速排序 快速排序是一种分治策略的排序算法,是由英国计算机科学家Tony Hoare发明的, 该算法被发布在1961年的Communications of the ACM 国际计算机学会月刊. 注:A ...

  5. 数据结构和算法(Golang实现)(24)排序算法-优先队列及堆排序

    优先队列及堆排序 堆排序(Heap Sort)由威尔士-加拿大计算机科学家J. W. J. Williams在1964年发明,它利用了二叉堆(A binary heap)的性质实现了排序,并证明了二叉 ...

  6. redis 的使用 (sort set排序集合类型操作)

    sort set排序集合类型 释义: sort set 是 string 类型的集合 sort set 的每个元素 都会关联一个 权 通过 权值 可以有序的获取集合中的元素 应用场合: 获取热门帖子( ...

  7. counting sort 计数排序

    //counting sort 计数排序 //参考算法导论8.2节 #include<cstdio> #include<cstring> #include<algorit ...

  8. Golang fmt包使用小技巧

    h1 { margin-top: 0.6cm; margin-bottom: 0.58cm; direction: ltr; color: #000000; line-height: 200%; te ...

  9. Collections.sort自定义排序的使用方法

    Collections.sort自定义排序的使用方法 总结:Collections可以对List进行排序:如果想对Map进行排序,可以将Map转化成List,进行排序: public static v ...

随机推荐

  1. python hdfs初体验

    新建目录 chr 新建文件hdfstest1.txt并写入内容 复制hdfstest1.txt的内容到hdfstest2.txt

  2. python 实例方法,类方法,静态方法,普通函数

    python中有实例方法,类方法,静态方法,普通函数 类方法需要@ classmethod 修饰并且有个隐藏参数 cls,实例方法必须有个参数 self, 静态方法必须有 @staticmethod修 ...

  3. python:lambda、filter、map、reduce

    lambda 为关键字.filter,map,reduce为内置函数. lambda:实现python中单行最小函数. g = lambda x: x * 2 #相当于 def g(x): retur ...

  4. 实验十一 团队作业7:团队项目设计完善&编码

    实验十一 团队作业7:团队项目设计完善&编码 实验时间 2019-6-6 Deadline: 2019-6-12 10:00,以团队随笔博文提交至班级博客的时间为准. 评分标准: 按时交 – ...

  5. 电商技术中企业数据总线ESB和注册服务管理的区别

    一.概述 1.什么是ESB 就是企业数据总线的意思,他的核心功能就是兼容各种协议接口,可以将数据在各种协议之间进行流转,并且可以针对数据格式进行编排转换. 异构系统,功能繁多,复杂 代表性的项目有:J ...

  6. shell脚本,锁机制

    [root@localhost wyb]# cat suijizhi.sh #!/bin/bash a=`|grep -v grep |wc -l` echo "$a" [ $a ...

  7. XML,面向对象基础

    什么是XML XML与JSON的对比 XML文档格式 使用XML模块解析 一,什么是XML ''' XML 全称可扩展标记语言 <tag></tag> 双标签 <tag/ ...

  8. LightOJ 1422 区间DP Halloween Costumes

    d(i, j)表示第i天到第j天至少要穿多少件衣服. 先不考虑第i天和后面 i+1 ~ j 天的联系,那就是至少要穿 1 + d(i+1, j)件衣服. 再看状态转移,如果后面第k(i+1 ≤ k ≤ ...

  9. 1,python初识

    什么是变量? 变量:将程序的中间结果暂时存储起来,以便后续程序调用. 什么是字符串类型? python中被引号引起来的数据就是字符串.字符串类型,也简称str类型. 在python中 int是什么? ...

  10. Maven部署异常:on project standalone-pom: Cannot deploy artifact from the local repository解决方法

    MAVEN部署异常 org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache. ...