非尔莫属 發表於 2019-8-16 17:47:00

Go排序

<p>本文链接:https://blog.csdn.net/u011304970/article/details/71447148</p>
<p>简介<br>
Go的sort包提供了排序功能。包括基本类型的排序和自定义数据(通常是结构体数组)的排序。</p>
<p>基本类型的排序<br>
sort提供了以下API对基本类型进行排序,查找</p>
<pre><code class="language-python">// 排序
func Ints(a []int)
func Float64s(a []float64)
func Strings(a []string)

// 判断是否已经排序
func IntsAreSorted(a []int) bool
func Float64sAreSorted(a []float64) bool
func StringsAreSorted(a []string) bool

// 在已排序的slice中查找,返回索引值,如果找不到,返回len(slice)
func SearchStrings(a []string, x string) int
func SearchFloat64s(a []float64, x float64) int
func SearchInts(a []int, x int) int
</code></pre>
<pre><code class="language-python">package main

import (
    "fmt"
    "sort"
)

func main() {
    is := []int{3, 5, 2}
    fmt.Println(sort.IntsAreSorted(is)) // false
    sort.Ints(is)
    fmt.Println(is)                     //
    fmt.Println(sort.IntsAreSorted(is)) // true
    fmt.Println(sort.SearchInts(is, 5)) // 2
    fmt.Println(sort.SearchInts(is, 8)) // 3
}

</code></pre>
<p>自定义类型的排序<br>
自定义类型的排序分为两种,一种是通过传递函数值,一种是通过实现接口</p>
<p>传递函数值</p>
<pre><code class="language-python">// 对slice排序
func Slice(slice interface{}, less func(i, j int) bool)
func SliceStable(slice interface{}, less func(i, j int) bool) // 稳定排序
// 判断是否已经排序
func SliceIsSorted(slice interface{}, less func(i, j int) bool) bool
// 二分法查找,这个函数很有意思,它查找并返回使函数f返回true和返回false的临界点(见下例)
func Search(n int, f func(int) bool) int
</code></pre>
<p>Slice()和SliceIsSorted()用法</p>
<pre><code class="language-python">package main

import (
    "fmt"
    "sort"
)

type Person struct {
    Name string
    Ageint
}

func main() {

    persons := []Person{
      Person{"Gopher", 11},
      Person{"Monkey", 12},
      Person{"Cynhard", 5},
    }

    // 按照名字排序
    less := func(i, j int) bool {
      return persons.Name &lt; persons.Name
    }

    fmt.Println(sort.SliceIsSorted(persons, less)) // false
    sort.Slice(persons, less)
    fmt.Println(persons)                           // [{Cynhard 5} {Gopher 11} {Monkey 12}]
    fmt.Println(sort.SliceIsSorted(persons, less)) // true
}
</code></pre>
<p>Search()用法</p>
<pre><code class="language-python">package main

import (
    "fmt"
    "sort"
)

func main() {

    ints := []int{22, 34, 21, 32, 54, 64, 49, 43}
    sort.Ints(ints)
    fmt.Println(ints) //

    // ints~ints都小于32,ints~ints都大于等于32
    // 因此临界点索引为2,found==2
    found := sort.Search(len(ints), func(i int) bool {
      return ints &gt;= 32
    })
    fmt.Println(found) // 2
    if found &lt; len(ints) &amp;&amp; ints == 32 {
      fmt.Printf("32 found at index %d\n", found)
    } else { // 没有找到32,但是返回了32应该插入的位置
      fmt.Println("32 not found")
    }

    // 由于找不到一个临界点,使序列左边为32,右边不为32
    // 所以返回len(ints),found==8
    found = sort.Search(len(ints), func(i int) bool {
      return ints == 32
    })
    fmt.Println(found) // 8
}
</code></pre>
<p>实现接口<br>
sort提供了一个接口:sort.Interface,所有满足这个接口的自定义类型都可以进行排序。API如下</p>
<pre><code class="language-python">type Interface interface {
      Len() int
      Less(i, j int) bool
      Swap(i, j int)
}
func Sort(data Interface)
func IsSorted(data Interface) bool
func Stable(data Interface)
// 返回data的逆序接口
func Reverse(data Interface) Interface
</code></pre>
<p>例如:</p>
<pre><code class="language-python">package main

import (
    "fmt"
    "sort"
)

type Person struct {
    Name string
    Ageint
}

type PerColl []Person

var persons PerColl

func (PerColl) Len() int {
    return len(persons)
}

func (PerColl) Less(i, j int) bool {
    return persons.Name &lt; persons.Name
}

func (PerColl) Swap(i, j int) {
    persons, persons = persons, persons
}

func main() {

    persons = []Person{
      Person{"Cynhard", 5},
      Person{"Gopher", 11},
      Person{"Monkey", 12},
    }

    sort.Sort(persons)
    fmt.Println(persons)                // [{Cynhard 5} {Gopher 11} {Monkey 12}]
    fmt.Println(sort.IsSorted(persons)) // true
    sort.Sort(sort.Reverse(persons))   
    fmt.Println(persons)                // [{Monkey 12} {Gopher 11} {Cynhard 5}]
}
</code></pre><br><br>
来源:https://www.cnblogs.com/nyist-xsk/p/11365447.html
頁: [1]
查看完整版本: Go排序