1. 程式人生 > >Golang對自定義型別排序

Golang對自定義型別排序

在實際專案中用到對結構按結構體中的某個欄位進行排序,在網上查到一個比較好的辦法,mark一下。

首先golang的sort包提供了基本的排序,包括插入排序(insertionSort)、歸併排序(symMerge)、堆排序(heapSort)和快速排序(quickSort)。其實現如下

func Sort(data Interface) {
    // Switch to heapsort if depth of 2*ceil(lg(n+1)) is reached.
    n := data.Len()
    maxDepth := 0
    for i := n; i > 0; i >>= 1 {
        maxDepth++
    }
    maxDepth *= 2
    quickSort(data, 0, n, maxDepth)
}

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)
}
// 內部實現的四種排序演算法
// 插入排序
func insertionSort(data Interface, a, b int)
// 堆排序
func heapSort(data Interface, a, b int)
// 快速排序
func quickSort(data Interface, a, b, maxDepth int)
// 歸併排序
func symMerge(data Interface, a, m, b int)

參照sort包對int型別的排序:

// 首先定義了一個[]int型別的別名IntSlice 
type IntSlice []int
// 獲取此 slice 的長度
func (p IntSlice) Len() int           { return len(p) }
// 比較兩個元素大小 升序
func (p IntSlice) Less(i, j int) bool { return p[i] < p[j] }
// 交換資料
func (p IntSlice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }
// sort.Ints()內部呼叫Sort() 方法實現排序
// 注意 要先將[]int 轉換為 IntSlice型別 因為此型別才實現了Interface的三個方法 
func Ints(a []int) { Sort(IntSlice(a)) }

可以實現如下程式碼:

/*
 對結構按其中一個欄位排序
 */

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 (s personSlice) Less(i, j int) bool { return s[i].Name > s[j].Name }

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)
}

上面僅按結構體中一個欄位進行排序,如果想基本多個欄位排序呢?答案是利用巢狀結構體實現,即定義基本的Len()和Swap()方法,然後基本巢狀結構封裝Less()比較方法。具體如下:

/*
  對結構按多欄位排序
 */

type student struct {
	Name string
 	Age int
}

type stus []student
func(s stus) Len() int { return len(s) }
func(s stus) Swap(i, j int) { s[i], s[j] = s[j], s[i] }

type sortByName struct{ stus }
// 按名字排序
func(m sortByName) Less(i, j int) bool {
	return m.stus[i].Name > m.stus[j].Name
}

type sortByAge struct { stus }
// 按年齡排序
func(m sortByAge) Less(i, j int) bool {
	return m.stus[i].Age > m.stus[j].Age
}

func main() {
	s := stus {
		{
			Name:"test123",
			Age:20,
		},
		{
			Name:"test1",
			Age:22,
		},
		{
			Name:"test21",
			Age:21,
		},
	}

	sort.Sort(sortByName{s})
	//sort.Stable(sortByAge{s})
	fmt.Println(s)
}