参考golang中国相关评论,发现golang是具备自定义key lambda函数的,只不过写法上需要实现golang官方排序接口,写起来也不难很优雅

package main

import (
    "fmt"
    "sort"
)

type Test struct {
    a  int
    b int
    c int
}
type Tests []Test

func (t Tests) Len() int {
    return len(t)
}

func (t Tests) Less(i, j int) bool {
    if t[i].a < t[j].a {
        return true
    } else if t[i].a > t[j].a {
        return false
    }

    if t[i].b < t[j].b {
        return true
    } else if t[i].b > t[j].b {
        return false
    }

    return t[i].c < t[j].c
}

func (t Tests) Swap(i, j int) {
    temp := t[i]
    t[i] = t[j]
    t[j] = temp
}

func Sort(t sort.Interface) {
    sort.Sort(t)
}

func main() {


    test := Tests{}

    test = append(test, Test{0, 95, 5})
    test = append(test, Test{1, 90, 3})
    test = append(test, Test{1, 100, 4})
    test = append(test, Test{2, 95, 5})
    test = append(test, Test{2, 95, 1})
    test = append(test, Test{1, 90, 4})
    test = append(test, Test{1, 100, 1})
    test = append(test, Test{2, 95, 2})
    test = append(test, Test{2, 95, 3})
    Sort(test)

    for _, t := range test {
        fmt.Println(t)
    }
}