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

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

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

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

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

// 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 對 []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 對常見的 []int []string 分別定義了 IntSlice StringSlice, 實現(xiàn)了各自的排序接口。而 sort.Ints 和 sort.Strings 可以直接對 []int 和 []string 進行排序, 使用起來非常方便 

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 進行逆序排序
如果我們想對一個 sortable object 進行逆序排序,可以自定義一個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 進行穩(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語言編程使用sort來排序示例詳解的詳細內(nèi)容,更多關于go語言sort排序的資料請關注腳本之家其它相關文章!

相關文章

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

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

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

    Go語言列表List獲取元素的4種方式

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

    go中的unsafe包及使用詳解

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

    Go語言中slice的用法實例分析

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

    Go語言實現(xiàn)開發(fā)一個簡單的gRPC Demo

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

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

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

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

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

    關于golang中死鎖的思考與學習

    本文主要介紹了關于golang中死鎖的思考與學習,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-03-03
  • golang通過gorm操作sqlite設置主鍵自增的步驟

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

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

    減少 golang 二進制文件大小操作

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

最新評論