前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Golang sort 包使用

Golang sort 包使用

作者头像
恋喵大鲤鱼
发布2019-06-14 20:46:36
5020
发布2019-06-14 20:46:36
举报
文章被收录于专栏:C/C++基础

1.sort包简介

Golang 中的标准库 sort 包为切片及用户定义的集合的排序操作提供了原语。sort包提供了对内置类型切片的排序支持,如 []int切片、[]float64切片和[]string切片。如果需要对自定义struct类型切片进行排序,需要实现接口sort.Interface的三个方法。

代码语言:javascript
复制
type Interface interface {
    // Len 为集合内元素的总数
    Len() int
	
    // Less 返回索引为 i 的元素是否应排在索引为 j 的元素之前。
    Less(i, j int) bool
	
    // Swap 交换索引为 i 和 j 的元素
    Swap(i, j int)
}

2.内置类型切片排序

代码语言:javascript
复制
//整数排序
ages := []int{2, 1, 5, 66, 55, 23}
sort.Ints(ages)
fmt.Println(ages)

//浮点数排序
float64Slice :=[]float64{1.1, 4.4, 2.2, 3.3, 5.5, 6.6}
sort.Float64s(float64Slice)
fmt.Println(float64Slice)

//字符串排序
names := []string{"Hello", "World", "private", "folders", "Users", "workspace"}
sort.Strings(names)
fmt.Println(value)

输出结果:

代码语言:javascript
复制
[1 2 5 23 55 66]
[1.1 2.2 3.3 4.4 5.5 6.6]
[Hello Users World folders private workspace]

如果想降序排序,需要借助 sort.Reverse() 函数。以[]int切片为例。

代码语言:javascript
复制
ages := []int{2, 1, 5, 66, 55, 23}
sort.Sort(sort.Reverse(sort.IntSlice(ages)))
fmt.Println(ages)

输出结果:

代码语言:javascript
复制
[66 55 23 5 2 1]

3.自定义struct类型切片的排序

根据自定义 struct Person 中的字段 bIsBefore 和 Age 进行排序,示例代码如下:

代码语言:javascript
复制
package main

import (
    "fmt"
    "sort"
)

type Person struct {
    Name string
    Age  int 
    bIsBefore bool
}

// ByAge implements sort.Interface for []Person based on the bIsBefore and Age field.
type ByAge []Person

func (a ByAge) Len() int           { return len(a) }
func (a ByAge) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
func (a ByAge) Less(i, j int) bool { 
        if a[i].bIsBefore {
                return true
        }   
        if a[j].bIsBefore {
                return false
        }   
        return a[i].Age > a[j].Age 
}

func main() {
    people := []Person{
        {"Bob", 31, true},
        {"John", 42, false},
        {"Michael", 17, false},
        {"Jenny", 42, false},
        {"Dable", 42, false},
        {"Dongdong", 25, false},
        {"Tommy", 42, false},
    }   

    fmt.Println(people)
    sort.Sort(ByAge(people))
    fmt.Println(people)
}

输出结果:

代码语言:javascript
复制
[{John 42 false} {Dable 42 false} {Michael 17 false} {Jenny 42 false} {King 13 false} {Tommy 50 false} {Bob 31 true}]
[{Bob 31 true} {Tommy 50 false} {Dable 42 false} {Jenny 42 false} {John 42 false} {Michael 17 false} {King 13 false}]

从排序结果可以看出,通过 sort.Sort() 进行排序,原本 John 在相等的 Dable 和 Jenny 前面,排序后出现在 Dable 和 Jenny 的后面,可见 sort.Sort() 是不稳定排序。如果想实现稳定排序,使用 sort.Stable(),排序结果如下:

代码语言:javascript
复制
[{John 42 false} {Dable 42 false} {Michael 17 false} {Jenny 42 false} {King 13 false} {Tommy 50 false} {Bob 31 true}]
[{Bob 31 true} {Tommy 50 false} {John 42 false} {Dable 42 false} {Jenny 42 false} {Michael 17 false} {King 13 false}]

参考文献

[1]Package sort

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年05月29日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.sort包简介
  • 2.内置类型切片排序
  • 3.自定义struct类型切片的排序
  • 参考文献
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档