欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

golang編程開發(fā)使用sort排序示例詳解

 更新時(shí)間:2021年11月15日 16:58:49   作者:秋天的春  
這篇文章主要為大家介紹了go語(yǔ)言編程開發(fā)使用sort來(lái)排序示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪

golang sort package: https://studygolang.com/articles/3360

sort 操作的對(duì)象通常是一個(gè) slice,需要滿足三個(gè)基本的接口,并且能夠使用整數(shù)來(lái)索引

// A type, typically a collection, that satisfies sort.Interface can be
// sorted by the routines in this package. The methods require that the
// elements of the collection be enumerated by an integer index. 
type Interface interface { 
// Len is the number of elements in the collection. 
Len() int 
// Less reports whether the element with 
// index i should sort before the element with index j. 
Less(i, j int) bool 
// Swap swaps the elements with indexes i and j. 
Swap(i, j int) 
}

ex-1 對(duì) []int 從小到大排序 

package main 
import ( 
"fmt" 
"sort" 
) 
type IntSlice []int
func (s IntSlice) Len() int { return len(s) }
func (s IntSlice) Swap(i, j int){ s[i], s[j] = s[j], s[i] } 
func (s IntSlice) Less(i, j int) bool { return s[i] < s[j] }
func main() { 
a := []int{4,3,2,1,5,9,8,7,6} 
sort.Sort(IntSlice(a)) 
fmt.Println("After sorted: ", a)
 
}

 ex-2 使用 sort.Ints 和 sort.Strings
golang 對(duì)常見的 []int []string 分別定義了 IntSlice StringSlice, 實(shí)現(xiàn)了各自的排序接口。而 sort.Ints 和 sort.Strings 可以直接對(duì) []int 和 []string 進(jìn)行排序, 使用起來(lái)非常方便 

package main 
import (
"fmt" 
"sort" 
)
func main() {
a := []int{3, 5, 4, -1, 9, 11, -14} 
sort.Ints(a) 
fmt.Println(a)
ss := []string{"surface", "ipad", "mac pro", "mac air", "think pad", "idea pad"} 
sort.Strings(ss)
fmt.Println(ss) 
sort.Sort(sort.Reverse(sort.StringSlice(ss))) 
fmt.Printf("After reverse: %v\n", ss) 
}
 

ex-3 使用 sort.Reverse 進(jìn)行逆序排序
如果我們想對(duì)一個(gè) sortable object 進(jìn)行逆序排序,可以自定義一個(gè)type。但 sort.Reverse 幫你省掉了這些代碼

package main 
import ( 
"fmt" 
"sort" 
)
func main() { 
a := []int{4,3,2,1,5,9,8,7,6} 
sort.Sort(sort.Reverse(sort.IntSlice(a)))
fmt.Println("After reversed: ", a)
 
}

ex-4 使用 sort.Stable 進(jìn)行穩(wěn)定排序
sort.Sort 并不保證排序的穩(wěn)定性。如果有需要, 可以使用 sort.Stable 

package main 
import ( 
"fmt" 
"sort" 
)
type person struct {
Name string
Age int
}
type personSlice []person 
func (s personSlice) Len() int { return len(s) }
func (s personSlice) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s personSlice) Less(i, j int) bool { return s[i].Age < s[j].Age } 
func main() {
a := personSlice {
{ 
Name: "AAA",
Age: 55,
}, 
{
Name: "BBB",
Age: 22,
}, 
{
Name: "CCC",
Age: 0,
},
{
Name: "DDD",
Age: 22,
},
{ 
Name: "EEE",
Age: 11, 
},
}
sort.Stable(a)
fmt.Println(a) 
}

以上就是go語(yǔ)言編程使用sort來(lái)排序示例詳解的詳細(xì)內(nèi)容,更多關(guān)于go語(yǔ)言sort排序的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Go語(yǔ)言實(shí)現(xiàn)二維數(shù)組的2種遍歷方式以及案例詳解

    Go語(yǔ)言實(shí)現(xiàn)二維數(shù)組的2種遍歷方式以及案例詳解

    這篇文章主要介紹了Go語(yǔ)言實(shí)現(xiàn)二維數(shù)組的2種遍歷方式以及案例詳解,圖文代碼聲情并茂,有感興趣的可以學(xué)習(xí)下
    2021-03-03
  • Go語(yǔ)言列表List獲取元素的4種方式

    Go語(yǔ)言列表List獲取元素的4種方式

    Golang的列表元素的獲取可以使用內(nèi)置的 Front 函數(shù)獲取頭結(jié)點(diǎn),使用 Back 函數(shù)獲取尾結(jié)點(diǎn),使用 Prev 獲取前一個(gè)結(jié)點(diǎn),使用 Next 獲取下一個(gè)結(jié)點(diǎn),本文就介紹了Go語(yǔ)言列表List獲取元素的4種方式,感興趣的可以了解一下
    2022-04-04
  • go中的unsafe包及使用詳解

    go中的unsafe包及使用詳解

    Unsafe code是一種繞過go類型安全和內(nèi)存安全檢查的Go代碼。這篇文章主要介紹了go中的unsafe包,需要的朋友可以參考下
    2019-07-07
  • Go語(yǔ)言中slice的用法實(shí)例分析

    Go語(yǔ)言中slice的用法實(shí)例分析

    這篇文章主要介紹了Go語(yǔ)言中slice的用法,實(shí)例分析了slice的功能及使用技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-02-02
  • Go語(yǔ)言實(shí)現(xiàn)開發(fā)一個(gè)簡(jiǎn)單的gRPC Demo

    Go語(yǔ)言實(shí)現(xiàn)開發(fā)一個(gè)簡(jiǎn)單的gRPC Demo

    這篇文章主要為大家詳細(xì)介紹了如何利用Go語(yǔ)言實(shí)現(xiàn)開發(fā)一個(gè)簡(jiǎn)單的gRPC Demo,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起了解一下
    2023-07-07
  • Go?gRPC進(jìn)階教程gRPC轉(zhuǎn)換HTTP

    Go?gRPC進(jìn)階教程gRPC轉(zhuǎn)換HTTP

    這篇文章主要為大家介紹了Go?gRPC進(jìn)階教程gRPC轉(zhuǎn)換HTTP教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-06-06
  • 利用Golang生成整數(shù)隨機(jī)數(shù)方法示例

    利用Golang生成整數(shù)隨機(jī)數(shù)方法示例

    這篇文章主要介紹了利用Golang生成整數(shù)隨機(jī)數(shù)的相關(guān)資料,文中給出了詳細(xì)的介紹和完整的示例代碼,相信對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-04-04
  • 關(guān)于golang中死鎖的思考與學(xué)習(xí)

    關(guān)于golang中死鎖的思考與學(xué)習(xí)

    本文主要介紹了關(guān)于golang中死鎖的思考與學(xué)習(xí),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2023-03-03
  • golang通過gorm操作sqlite設(shè)置主鍵自增的步驟

    golang通過gorm操作sqlite設(shè)置主鍵自增的步驟

    這篇文章主要介紹了golang通過gorm操作sqlite設(shè)置主鍵自增的詳細(xì)步驟,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-09-09
  • 減少 golang 二進(jìn)制文件大小操作

    減少 golang 二進(jìn)制文件大小操作

    這篇文章主要介紹了減少 golang 二進(jìn)制文件大小操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-12-12

最新評(píng)論