golang: 使用 sort 来排序
golang sort package: https://golang.org/src/sort
sort 操作的对象通常是一个 slice,需要满足三个基本的接口,并且能够使用整数来索引
// 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