原创不易,未经允许,请勿转载。

intfloatsort
// Ints sorts a slice of ints in increasing order.
func Ints(a []int) { Sort(IntSlice(a)) }

// Float64s sorts a slice of float64s in increasing order
// (not-a-number values are treated as less than other values).
func Float64s(a []float64) { Sort(Float64Slice(a)) }

// Strings sorts a slice of strings in increasing order.
func Strings(a []string) { Sort(StringSlice(a)) }
Float64SliceIntSliceSortInterface
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)
}

type IntSlice []int

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 is a convenience method.
func (p IntSlice) Sort() { Sort(p) }

// Float64Slice attaches the methods of Interface to []float64, sorting in increasing order
// (not-a-number values are treated as less than other values).
type Float64Slice []float64

func (p Float64Slice) Len() int           { return len(p) }
func (p Float64Slice) Less(i, j int) bool { return p[i] < p[j] || isNaN(p[i]) && !isNaN(p[j]) }
func (p Float64Slice) Swap(i, j int)      { p[i], p[j] = p[j], p[i] }

所以如果我们要实现自定义的排序规则时,可以自定义类型,然后去实现接口中的这三个方法即可。
加入需要按照学生的名字进行排序,我们可以这么写

type Students []Student

func (s Students) Len() int {
	return len(s)
}

func (s Students) Swap(i, j int) {
	s[i], s[j] = s[j], s[i]
}
func (s Students) Less(i, j int) bool {
	return s[i].Name < s[j].Name
}

func main() {
	stus := []Student{
		{Name: "zhangsan"},
		{Name: "lisi"},
		{Name: "wangwu"},
	}
	sort.Sort(Students(stus))
	for _, val := range stus {
		fmt.Println(val)
	}
}

/*output
{lisi }
{wangwu }
{zhangsan }
*/
Less(i,j int)boolsortsort.Slice()
func main() {
	stus := []Student{
		{Name: "zhangsan"},
		{Name: "lisi"},
		{Name: "wangwu"},
	}
	sort.Slice(stus, func(i, j int) bool {
		return stus[i].Name < stus[j].Name
	})
	for _, val := range stus {
		fmt.Println(val)
	}

}
sort.Slice()sort.SliceStable()

如果这篇文章对您有所帮助,麻烦点个一键三连。

原创不易,未经允许,请勿转载。