鍍金池/ 教程/ GO/ Go按自定義函數(shù)排序?qū)嵗?/span>
Go panic錯誤處理實例
Go命令行參數(shù)實例
Go可變參數(shù)的函數(shù)實例
Go通道同步實例
Go非阻塞通道操作實例
Go指針實例
Go數(shù)字解析實例
Go語言指針
Go超時(timeouts)實例
Go速率限制實例
Go信號實例
Go Base64編碼實例
Go計時器實例
Go命令行標(biāo)志實例
Go原子計數(shù)器實例
Go語言切片
Go隨機(jī)數(shù)實例
Go語言類型轉(zhuǎn)換
Go排序?qū)嵗?/span>
Go時間格式化/解析實例
Go URL解析實例
Go字符串函數(shù)實例
Go語言常量
Go for循環(huán)語句實例
Go函數(shù)多個返回值實例
Go切片實例
Go行過濾器實例
Go語言接口
Go語言數(shù)組
Go語言變量
Go字符串格式化實例
Go斷續(xù)器實例
Go if/else語句實例
Go通道緩沖實例
Go錯誤實例
Go語言映射
Go執(zhí)行過程實例
Go函數(shù)實例
Go有狀態(tài)的goroutines實例
Go按自定義函數(shù)排序?qū)嵗?/span>
Go語言作用域規(guī)則
Go時代(Epoch)實例
Go變量實例
Go互斥體實例
Go語言范圍(range)
Go程序?qū)嵗?/span>
Go語言入門
Go通道路線實例
Go閉包(匿名函數(shù))實例
Go Select實例
Go通道范圍實例
Go集合函數(shù)實例
Hello World程序?qū)嵗?/span>
Go環(huán)境變量實例
Go語言運算符
Go讀取文件實例
Go延遲(defer)實例
Go SHA1哈希實例
Go語言條件和決策
Go語言錯誤處理
Go通道實例
Go指針實例
Go時間日期實例
Go語言字符串
Go語言循環(huán)
Go語言基礎(chǔ)語法
Go語言開發(fā)環(huán)境安裝配置
Go常量實例
Go語言結(jié)構(gòu)體
Go寫文件實例
Go正則表達(dá)式實例
Go JSON實例
Go語言教程
Go關(guān)閉通道實例
Go接口實例
Go語言遞歸
Go switch語句實例
Go函數(shù)遞歸實例
Go退出程序?qū)嵗?/span>
Go語言程序結(jié)構(gòu)
Go范圍實例
Go語言函數(shù)
Go工作池實例
Go語言數(shù)據(jù)類型

Go按自定義函數(shù)排序?qū)嵗?/h1>

有時候,我們希望通過除了自然順序以外的其他方式對集合進(jìn)行排序。例如,假設(shè)我們想按字符串的長度而不是字母順序?qū)ψ址M(jìn)行排序。下面是Go語言中自定義排序的示例。

為了使用Go語言中的自定義函數(shù)進(jìn)行排序,我們需要一個相應(yīng)的類型。這里創(chuàng)建了一個ByLength類型,它只是內(nèi)置 []string類型的別名。

需要實現(xiàn)sort.Interface - Len,LessSwap - 在這個類型上,所以可以使用 sort 包中的一般Sort函數(shù)。LenSwap通常在類型之間是相似的,Less保存實際的自定義排序邏輯。在這個例子中,要按照字符串長度的增加順序排序,因此在這里使用len(s [i])len(s [j])

所有這些都到位后,現(xiàn)在可以通過將原始 fruits 切片轉(zhuǎn)換為ByLength來實現(xiàn)自定義排序,然后對該類型切片使用sort.Sort()方法。

運行程序根據(jù)需要顯示按字符串長度排序的列表。

通過遵循創(chuàng)建自定義類型的模式,在該類型上實現(xiàn)三個Interface方法,然后在自定義類型的集合上調(diào)用sort.Sort,可以通過任意函數(shù)對Go切片進(jìn)行排序。

所有的示例代碼,都放在 F:\worksp\golang 目錄下。安裝Go編程環(huán)境請參考:http://www.yiibai.com/go/go_environment.html

sorting-by-functions.go的完整代碼如下所示 -

package main

import "sort"
import "fmt"

// In order to sort by a custom function in Go, we need a
// corresponding type. Here we've created a `ByLength`
// type that is just an alias for the builtin `[]string`
// type.
type ByLength []string

// We implement `sort.Interface` - `Len`, `Less`, and
// `Swap` - on our type so we can use the `sort` package's
// generic `Sort` function. `Len` and `Swap`
// will usually be similar across types and `Less` will
// hold the actual custom sorting logic. In our case we
// want to sort in order of increasing string length, so
// we use `len(s[i])` and `len(s[j])` here.
func (s ByLength) Len() int {
    return len(s)
}
func (s ByLength) Swap(i, j int) {
    s[i], s[j] = s[j], s[i]
}
func (s ByLength) Less(i, j int) bool {
    return len(s[i]) < len(s[j])
}

// With all of this in place, we can now implement our
// custom sort by casting the original `fruits` slice to
// `ByLength`, and then use `sort.Sort` on that typed
// slice.
func main() {
    fruits := []string{"peach", "banana", "kiwi"}
    sort.Sort(ByLength(fruits))
    fmt.Println(fruits)
}

執(zhí)行上面代碼,將得到以下輸出結(jié)果 -

F:\worksp\golang>go run sorting-by-functions.go
[kiwi peach banana]