0
点赞
收藏
分享

微信扫一扫

GoLang标准库sort包

残北 2022-04-18 阅读 75
golang

文章目录

GoLang之标准库sort包

本文基于Windos系统上Go SDK v1.8进行讲解

1.sort包介绍

image-20220111105245176

2.sort.Interface接口

image-20220209211907554

image-20220209213239404

3.sort.IntSlice切片类型

3.1IntSlice切片类型

image-20220209212007770

3.2Len、Less、Swap、Sort方法

image-20220209212128002

image-20220111114348510

4.sort.Float64Slice切片类型

4.1Float64Slice类型

// Float64Slice implements Interface for a []float64, sorting in increasing order,
// with not-a-number (NaN) values ordered before other values.
type Float64Slice []float64

4.2Len、Less、Swap、Sort方法

func (x Float64Slice) Len() int { return len(x) }

// Less reports whether x[i] should be ordered before x[j], as required by the sort Interface.
// Note that floating-point comparison by itself is not a transitive relation: it does not
// report a consistent ordering for not-a-number (NaN) values.
// This implementation of Less places NaN values before any others, by using:
//
//	x[i] < x[j] || (math.IsNaN(x[i]) && !math.IsNaN(x[j]))
//
func (x Float64Slice) Less(i, j int) bool { return x[i] < x[j] || (isNaN(x[i]) && !isNaN(x[j])) }
func (x Float64Slice) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }


// Sort is a convenience method: x.Sort() calls Sort(x).
func (x Float64Slice) Sort() { Sort(x) }

5.sort.StringSlice切片类型

5.1StringSlice切片类型

// StringSlice attaches the methods of Interface to []string, sorting in increasing order.
type StringSlice []string

5.2Len、Less、Swap、Sort方法

func (x StringSlice) Len() int           { return len(x) }
func (x StringSlice) Less(i, j int) bool { return x[i] < x[j] }
func (x StringSlice) Swap(i, j int)      { x[i], x[j] = x[j], x[i] }

// Sort is a convenience method: x.Sort() calls Sort(x).
func (x StringSlice) Sort() { Sort(x) }

image-20220111114306212

6.sort.Sort函数

image-20220209211824774

7.sort.Ints函数

image-20220209213539682

image-20220111113522319

8.sort.Float64s函数

image-20220209213607366

image-20220111113713743

9.sort.Strings函数

image-20220209213623907

image-20220111113754594

10.sort.IsSorted函数

11.sort.Reverse函数

image-20220111112744280

image-20220111112629578

12.sort.Slice函数

image-20220127135655681

image-20220127135910777

image-20220127140255753

image-20220127141105646

image-20220303215625578

举报

相关推荐

0 条评论