gobyexample_sorting

========

参考网站

========

go 排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// GoByExample_Sorting project main.go

// Go's `sort` package implements sorting for builtins
// and user-defined types. We'll look at sorting for
// builtins first.
// go语言的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.
// 内置类型都有特定的排序方法。这里是一个string类型的示例。
// 注意排序是就地排序,所以会改变传入的切片,不返回一个新切片。
strs := []string{"c", "a", "b"}
sort.Strings(strs)
fmt.Println("Strings:", strs)

// An example of sorting `int`s.
// 一个对int型排序的示例。
ints := []int{7, 2, 4}
sort.Ints(ints)
fmt.Println("Ints: ", ints)

// We can also use `sort` to check if a slice is
// already in sorted order.
// 我们也可以用sort包来检查一个切片是否是排序好的。
s := sort.IntsAreSorted(ints)
fmt.Println("Sorted: ", s)
}

// 输出
//Ints: [2 4 7]
//Sorted: true
Fork me on GitHub