go实例之排序
1、默认排序
使用sort包进行排序。排序是就地排序,因此它会更改给定的切片,并且不返回新的切片。
package main import "fmt"
import "sort" func main() { // Sort methods are specific to the builtin type;
// here's an example for strings. Note that sorting is
// in-place, so it changes the given slice and doesn't
// return a new one.
strs := []string{"c", "a", "b"}
sort.Strings(strs)
fmt.Println("Strings:", strs) // An example of sorting `int`s.
ints := []int{, , }
sort.Ints(ints)
fmt.Println("Ints: ", ints) // We can also use `sort` to check if a slice is
// already in sorted order.
s := sort.IntsAreSorted(ints)
fmt.Println("Sorted: ", s)
}
执行上面代码,将得到以下输出结果
Strings: [a b c]
Ints: [ ]
Sorted: true
从上述代码可知,排序不同类型切片,调用不同接口,排序时直接对参数进行修改,排序接口不对排序后切片进行返回。
2、自定义排序
自定义排序需要我们声明一个相应类型,并实现sort.Interface-Len,Less,Swap在这个类型上,Less接口是正真的排序方法,Swap用于交换两个值,Len用于求出切片大小。如下示例代码,首先将原始 fruits 切片转换为ByLength来实现自定义排序,然后对该类型切片使用sort.Sort()方法。

图1 sort.Interface定义
其实自定义排序就是对接口的使用。针对ByLength结构重写了sort.Interface接口的方法。如图1所示,是在LiteIDE中sort.Interface的提示
package main import "sort"
import "fmt" // In order to sort by a custom function in Go, we need a
// corresponding type. Here we've created a `ByLength`
// type that is just an alias for the builtin `[]string`
// type.
type ByLength []string // We implement `sort.Interface` - `Len`, `Less`, and
// `Swap` - on our type so we can use the `sort` package's
// generic `Sort` function. `Len` and `Swap`
// will usually be similar across types and `Less` will
// hold the actual custom sorting logic. In our case we
// want to sort in order of increasing string length, so
// we use `len(s[i])` and `len(s[j])` here.
func (s ByLength) Len() int {
return len(s)
}
func (s ByLength) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s ByLength) Less(i, j int) bool {
return len(s[i]) < len(s[j])
} // With all of this in place, we can now implement our
// custom sort by casting the original `fruits` slice to
// `ByLength`, and then use `sort.Sort` on that typed
// slice.
func main() {
fruits := []string{"peach", "banana", "kiwi"}
sort.Sort(ByLength(fruits))
fmt.Println(fruits)
}
执行上面代码,将得到以下输出结果
[kiwi peach banana]
go实例之排序的更多相关文章
- C 语言实例 - 字符串排序
C 语言实例 - 字符串排序 C 语言实例 C 语言实例 按字典顺序排序. 实例 #include<stdio.h> #include <string.h> int main( ...
- SQL SERVER-修改实例的排序规则
系统库是无法直接修改的,只能修改用户数据库排序规则(要先解决依赖项.如表函数): ALTER DATABASE [xx] COLLATE Chinese_PRC_CI_AS 修改实例的排序规则,使用安 ...
- 修改Sqlserver实例默认排序规则
1.将sqlserver安装盘加载到虚拟光驱,这里加载到F:盘跟目录 2.cmd进入命令 3.输入命令: F:/Setup /QUIET /ACTION=REBUILDDATABASE /INSTAN ...
- 【从零学习经典算法系列】分治策略实例——高速排序(QuickSort)
在前面的博文(http://blog.csdn.net/jasonding1354/article/details/37736555)中介绍了作为分治策略的经典实例,即归并排序.并给出了递归形式和循环 ...
- SQLSERVER 修改数据实例的排序规则
SQL Server服务器修改排序规则的方法 操作及验证步骤: 1 登录数据库后,查看当前安装数据库默认排序规则的两种方式 方式一.使用SQL Server 2014 Management Studi ...
- SqlServer nvarchar中的中文字符匹配,更改SqlServer实例和数据库排序规则的办法
我们都知道在SqlServer中的nvarchar类型可以完美的存储诸如中文这种unicode字符,但是我们会发现有时候查询语句去查询nvarchar列的时候查不出来. 为什么nvarchar类型有时 ...
- 修改sql server实例、数据库、表、字段的排序规则
转自:http://blog.51cto.com/jimshu/1095780 概念与详情请参考:字符编码与排序规则:https://www.cnblogs.com/gered/p/9145123.h ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 网格系统实例:列排序
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Flex Array内置排序方法的使用
在Array类中,提供内置的排序方法.排序是在软件开发的过程中,经常遇到的问题.通过这些内置的方法,可以快速轻便的进行排序操作. Array类提供sort方法对Array实例进行排序.sort方法没有 ...
随机推荐
- 小随笔:利用Shader给斯坦福兔子长毛和实现雪地效果
0x00 前言 发现最近没有了写长篇大论的激情,可能是到了冬天了吧.所以这篇小文只是简单介绍下如何在Unity中利用shader很简单的实现雪地效果以及毛皮效果,当然虽然标题写在了一起,但其实这是俩事 ...
- 软件测试博客日记Day02-11.16日 —— 赵天宇 —— 禅道的使用和配置
禅道 1. 安装 1. 进入禅道的官方下载地址:http://www.zentao.net/download/80053.html 2. 下载禅道开源版本. 3. 正常安装,注意一定要放在根目录下. ...
- EasyUI 树菜单
EasyUI 树菜单 通过ssm框架项目实现EasyUI 的树菜单的单选,复选,异步加载树,同步加载树和树权限控制等功能. 本章知识点 效果图: 需求:通过SSM框架,实现EasyUI 树菜单的单选, ...
- [译]漫画SELinux概念
h2:first-child, body>h1:first-child, body>h1:first-child+h2, body>h3:first-child, body>h ...
- Web Mining and Big Data 公开课学习笔记 ---lecture1
1.1 LOOK Finding "stuff" on the web or computer or room or hidden in data Finding documen ...
- 51Nod1136--欧拉函数
1136 欧拉函数 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 对正整数n,欧拉函数是少于或等于n的数中与n互质的数的数目.此函数以其首名研究者欧拉命 ...
- Elasticsearch的基友Logstash
Logstash 是一款强大的数据处理工具,它可以实现数据传输,格式处理,格式化输出,还有强大的插件功能,常用于日志处理. 一.原理 Input 可以从文件中.存储中.数据库中抽取数据,Input有两 ...
- PostgreSQL启动main函数都干了什么(一)
DB Version:9.5.3 环境:CentOS7.x 调试工具:GDB source:src/backend/main/main.c 56 /* 57 * Any Postgres server ...
- 【下一代核心技术DevOps】:(一)容器服务的Rancher选型
为什么说是下一代核心技术 其实经过互联网的多次变革说起,早期的C/S架构,到后来的B/S架构,一直到现在最普遍的M/S架构,他们的背后都是技术不断的优化改进,以适应促进IT技术的发展 整体而言在过去1 ...
- slurm-16.05.3任务调度系统部署与测试(1)
1.概述2.同步节点时间3.下载并解压文件4.编译安装munge-0.5.125.配置munge6.编译安装slurm-16.05.37.配置slurm8.配置MySQL数据库环境9.启动slur ...