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实例之排序的更多相关文章

  1. C 语言实例 - 字符串排序

    C 语言实例 - 字符串排序 C 语言实例 C 语言实例 按字典顺序排序. 实例 #include<stdio.h> #include <string.h> int main( ...

  2. SQL SERVER-修改实例的排序规则

    系统库是无法直接修改的,只能修改用户数据库排序规则(要先解决依赖项.如表函数): ALTER DATABASE [xx] COLLATE Chinese_PRC_CI_AS 修改实例的排序规则,使用安 ...

  3. 修改Sqlserver实例默认排序规则

    1.将sqlserver安装盘加载到虚拟光驱,这里加载到F:盘跟目录 2.cmd进入命令 3.输入命令: F:/Setup /QUIET /ACTION=REBUILDDATABASE /INSTAN ...

  4. 【从零学习经典算法系列】分治策略实例——高速排序(QuickSort)

    在前面的博文(http://blog.csdn.net/jasonding1354/article/details/37736555)中介绍了作为分治策略的经典实例,即归并排序.并给出了递归形式和循环 ...

  5. SQLSERVER 修改数据实例的排序规则

    SQL Server服务器修改排序规则的方法 操作及验证步骤: 1 登录数据库后,查看当前安装数据库默认排序规则的两种方式 方式一.使用SQL Server 2014 Management Studi ...

  6. SqlServer nvarchar中的中文字符匹配,更改SqlServer实例和数据库排序规则的办法

    我们都知道在SqlServer中的nvarchar类型可以完美的存储诸如中文这种unicode字符,但是我们会发现有时候查询语句去查询nvarchar列的时候查不出来. 为什么nvarchar类型有时 ...

  7. 修改sql server实例、数据库、表、字段的排序规则

    转自:http://blog.51cto.com/jimshu/1095780 概念与详情请参考:字符编码与排序规则:https://www.cnblogs.com/gered/p/9145123.h ...

  8. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 网格系统实例:列排序

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. Flex Array内置排序方法的使用

    在Array类中,提供内置的排序方法.排序是在软件开发的过程中,经常遇到的问题.通过这些内置的方法,可以快速轻便的进行排序操作. Array类提供sort方法对Array实例进行排序.sort方法没有 ...

随机推荐

  1. sqoop: mysql to hive

    sqoop import --connect 数据库连接 --username 数据库用户名--password 数据库密码 --table 导入的表 -m 1 --hive-import --tar ...

  2. Python [习题] 字典排序

    [习题] 对此字典分别按照value 和key 如何排序? dic1 = {'and':40, 'a':54, 'is':60, 'path':139, 'the':124, 'os':49} In ...

  3. Windows下安装BeautifulSoup

    python版本为2.7 1.去官网下载BeautifulSoup4 Beautiful Soup 4.3.2 2.解压文件 将下载得到的压缩包解压到任意文件夹,路径不含中文 3.打开cmd命令提示符 ...

  4. ORACLE 错误代码提示归集

    有时数据库出现问题,不是每次都有网络可查,所以把所有的ora系列的错误整理出来, 在最没有办法的时候,需要自己来解决,有了这些根据,问题会好办的.虽说对于数据库方面, DBA很强大,他们在遇到错误时, ...

  5. MongoDB一:入门(安装与配置)

    一.简介 MongoDB  是一个基于分布式文件存储的数据库.由C++语言编写.旨在为WEB应用提供可扩展的高性能数据存储解决方案. mongoDB MongoDB 是一个介于关系数据库和非关系数据库 ...

  6. Spring集成Redis缓存

    作者:13 GItHub:https://github.com/ZHENFENG13 版权声明:本文为原创文章,未经允许不得转载. 整合Redis 本来以为类似的Redis教程和整合代码应该会很多,因 ...

  7. Hexo + github 打造个人博客

    前两年开始用 wordpress 搭了一个网站,但服务器是在 Linode 上,之所以要放在 Linode 上,要从买的域名说起,因为我买的域名是 fengzheng.pub ,.pub 是不允许备案 ...

  8. Python函数篇(5)-装饰器及实例讲解

    1.装饰器的概念   装饰器本质上就是一个函数,主要是为其他的函数添加附加的功能,装饰器的原则有以下两个: 装饰器不能修改被修饰函数的源代码 装饰器不能修改被修改函数的调用方式   装饰器可以简单的理 ...

  9. C#设计模式之二十策略模式(Stragety Pattern)【行为型】

    一.引言   今天我们开始讲"行为型"设计模式的第七个模式,该模式是[策略模式],英文名称是:Stragety Pattern.在现实生活中,策略模式的例子也非常常见,例如,在一个 ...

  10. Ext表单验证

    //大家在很多的extjs代码中都看到了这两个,他们都起提示作用的Ext.QuickTips.init();//支持tips提示Ext.form.Field.prototype.msgTarget=' ...