Go Programming Example [Sorting]

Reading table of contents

Go's sort package implements sorting functions for built-in and user-defined data types.

We first focus on the ordering of built-in data types.

package main

import (
	"fmt"
	"sort"
)

func main() {
    
    

	// 排序方法是针对内置数据类型的;
	// 这里是一个字符串的例子。
	// 注意排序是原地更新的,所以他会改变给定的序列并且不返回一个新值。
	strs := []string{
    
    "c", "a", "b"}
	sort.Strings(strs)
	fmt.Println("Strings:", strs)

	// 一个 `int` 排序的例子。
	ints := []int{
    
    7, 2, 4}
	sort.Ints(ints)
	fmt.Println("Ints:   ", ints)

	// 我们也可以使用 `sort` 来检查一个序列是不是已经是排好序的。
	s := sort.IntsAreSorted(ints)
	fmt.Println("Sorted: ", s)
}

Running the program prints the sorted sequence of strings and integers and the result of our AreSorted test is true.

[root@bogon test]# go run main.go 
Strings: [a b c]
Ints:    [2 4 7]
Sorted:  true
[root@bogon test]# 

Guess you like

Origin blog.csdn.net/weiguang102/article/details/129751413