golang编程开发使用sort排序示例详解
作者:秋天的春 发布时间:2023-09-16 23:20:18
标签:Golang,sort,排序
golang sort package: https://studygolang.com/articles/3360
sort 操作的对象通常是一个 slice,需要满足三个基本的接口,并且能够使用整数来索引
// A type, typically a collection, that satisfies sort.Interface can be
// sorted by the routines in this package. The methods require that the
// elements of the collection be enumerated by an integer index.
type Interface interface {
// Len is the number of elements in the collection.
Len() int
// Less reports whether the element with
// index i should sort before the element with index j.
Less(i, j int) bool
// Swap swaps the elements with indexes i and j.
Swap(i, j int)
}
ex-1 对 []int 从小到大排序
package main
import (
"fmt"
"sort"
)
type IntSlice []int
func (s IntSlice) Len() int { return len(s) }
func (s IntSlice) Swap(i, j int){ s[i], s[j] = s[j], s[i] }
func (s IntSlice) Less(i, j int) bool { return s[i] < s[j] }
func main() {
a := []int{4,3,2,1,5,9,8,7,6}
sort.Sort(IntSlice(a))
fmt.Println("After sorted: ", a)
}
ex-2 使用 sort.Ints 和 sort.Strings
golang 对常见的 []int
和 []string
分别定义了 IntSlice
和 StringSlice
, 实现了各自的排序接口。而 sort.Ints 和 sort.Strings 可以直接对 []int 和 []string 进行排序, 使用起来非常方便
package main
import (
"fmt"
"sort"
)
func main() {
a := []int{3, 5, 4, -1, 9, 11, -14}
sort.Ints(a)
fmt.Println(a)
ss := []string{"surface", "ipad", "mac pro", "mac air", "think pad", "idea pad"}
sort.Strings(ss)
fmt.Println(ss)
sort.Sort(sort.Reverse(sort.StringSlice(ss)))
fmt.Printf("After reverse: %v\n", ss)
}
ex-3 使用 sort.Reverse 进行逆序排序
如果我们想对一个 sortable object 进行逆序排序,可以自定义一个type。但 sort.Reverse 帮你省掉了这些代码
package main
import (
"fmt"
"sort"
)
func main() {
a := []int{4,3,2,1,5,9,8,7,6}
sort.Sort(sort.Reverse(sort.IntSlice(a)))
fmt.Println("After reversed: ", a)
}
ex-4 使用 sort.Stable 进行稳定排序
sort.Sort 并不保证排序的稳定性。如果有需要, 可以使用 sort.Stable
package main
import (
"fmt"
"sort"
)
type person struct {
Name string
Age int
}
type personSlice []person
func (s personSlice) Len() int { return len(s) }
func (s personSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s personSlice) Less(i, j int) bool { return s[i].Age < s[j].Age }
func main() {
a := personSlice {
{
Name: "AAA",
Age: 55,
},
{
Name: "BBB",
Age: 22,
},
{
Name: "CCC",
Age: 0,
},
{
Name: "DDD",
Age: 22,
},
{
Name: "EEE",
Age: 11,
},
}
sort.Stable(a)
fmt.Println(a)
}
来源:https://blog.csdn.net/ffzhihua/article/details/83858464
0
投稿
猜你喜欢
- 在讲CSS优先级之前,我们得要了解什么是CSS,CSS是用来做什么的。首先,我们对CSS作一个简单的说明:CSS是层叠样式表(Cascadi
- try ...except 是最常见的捕获处理异常的结构,其主要作用是将可能出现问题的代码块用try :包裹起来,不至于出现错误让程序崩溃,
- 介绍这道题是这样的,有一个二叉树,让求出这颗Bt树里面最大的宽度是有几个节点,同时还要求出最大宽度的这些节点在第几层?比如:下面这颗树,它每
- 训练好了model后,可以通过python调用caffe的模型,然后进行模型测试的输出。本次测试主要依靠的模型是在caffe模型里面自带训练
- xhEditor简介xhEditor是一个基于jQuery开发的简单迷你并且高效的可视化HTML编辑器,基于网络访问并且兼容IE 6.0+,
- type 所有类是type生成的a = 1b = "abc"print("type a:{}&qu
- 遍历目录或遍历目录下指定类型的文件,这是每一个童鞋在写程序的时候难免会用到的。PHP本身也提供了很多灰常有用的函数,正确地使用它们,不会有错
- 前言mpvue 是美团开源的一套语法与vue.js一致的、快速开发小程序的前端框架,按官网说可以达到小程序与H5界面使用一套代码。使用此框架
- 在例子视图中返回文本的方式有点特别,即HTML被直接硬编码在Python代码之中。def current_datetime(request)
- 为什么是Python先来聊聊为什么做数据分析一定要用Python或R语言。编程语言这么多种,Java, PHP都很成熟,但是为什么在最近热火
- 说下防止PHPDDOS发包的方法 if (eregi("ddos-udp",$read)) { fputs($verbi
- 通过一个demo测试这三个属性的差别。说明:scrollWidth:对象的实际内容的宽度,不包边线宽度,会随对象中内容超过可视区后而变大。
- 0. 引言马上314情人节就要来了,是否需要一首歌来抚慰你,受伤或躁动的心灵。来吧,今天教你用15行代码搞定热门歌单。学起来并听起来吧。本文
- python 实现文件查找和某些项输出本文是基于给定一文件(students.txt),查找其中GPA分数最高的 输出,同时输出其对应的姓名
- 遇到的问题:用户批量导入数据1000条,导入不成功的问题,提示查询不到商品资料。这个场景需要依靠批量的数据,每次测试的时候需要手动生成批量的
- import numpy as npimport sysdef conv_(img, conv_filter): filter_
- 离散特征的编码分为两种情况:1、离散特征的取值之间没有大小的意义,比如color:[red,blue],那么就使用one-hot编码2、离散
- 这是 2020 年第 3 个版本,也是最后一个版本。在 GoLand 2020.3 中,您可以探索 goroutines dumps,运行并
- 代码如下:'===================================== '获得文件后缀 '=====
- 上篇博客转载了关于感知器的用法,遂这篇做个大概总结,并实现一个简单的感知器,也为了加深自己的理解。感知器是最简单的神经网络,只有一层。感知器