23 September 2017

Table of Contents

sort

type User struct {
      name string
      age  int
}

type UserList []User

type SortByFunc func(u1 User, u2 User) int

func (u UserList) Len() int {
      return len(u)
}

func (u UserList) Swap(i int, j int) {
      u[i], u[j] = u[j], u[i]
}

func (u UserList) Less(i int, j int) bool {
      return SortByName(u[i], u[j])
}

func SortByName(u1 User, u2 User) bool {
      return u1.name < u2.name
}

func main() {
      userlist := []User{
              {"one", 1},
              {"two", 2},
      }

      fmt.Printf("%v\n", userlist)
      sort.Sort(UserList(userlist))
      fmt.Printf("%v\n", userlist)
}

sortByKeys

// User
type User struct {
      name string
      age  int
}

type UserList []User

func SortByName(u1 User, u2 User) bool {
      return u1.name < u2.name
}

func SortByAge(u1 User, u2 User) bool {
      return u1.age < u2.age
}

// UserSorter
type SortByFunc func(u1 User, u2 User) bool

type UserSorter struct {
      data     UserList
      sortFunc SortByFunc
}

func (s UserSorter) Len() int {
      return len(s.data)
}

func (s UserSorter) Swap(i int, j int) {
      s.data[i], s.data[j] = s.data[j], s.data[i]
}

func (s UserSorter) Less(i int, j int) bool {
      return s.sortFunc(s.data[i], s.data[j])
}

func main() {
      userlist := []User{
              {"a", 4},
              {"c", 2},
              {"d", 1},
              {"b", 3},
      }

      nameSorter := UserSorter{
              userlist,
              SortByName,
      }

      ageSorter := UserSorter{
              userlist,
              SortByAge,
      }

      sort.Sort(UserSorter(nameSorter))
      fmt.Printf("%v\n", userlist)

      sort.Sort(UserSorter(ageSorter))
      fmt.Printf("%v\n", userlist)
}

SortMultiKeys

// User
type User struct {
        name string
        age  int
}

type UserList []User

func SortByName(u1 User, u2 User) bool {
        return u1.name < u2.name
}

func SortByAge(u1 User, u2 User) bool {
        return u1.age < u2.age
}

func SortByNameAge(u1 User, u2 User) bool {
        if u1.name == u2.name {
                return SortByAge(u1, u2)
        }
        return SortByName(u1, u2)
}

// UserSorter
type SortByFunc func(u1 User, u2 User) bool

type UserSorter struct {
        data     UserList
        sortFunc SortByFunc
}

func (s UserSorter) Len() int {
        return len(s.data)
}

func (s UserSorter) Swap(i int, j int) {
        s.data[i], s.data[j] = s.data[j], s.data[i]
}

func (s UserSorter) Less(i int, j int) bool {
        return s.sortFunc(s.data[i], s.data[j])
}

func main() {
        userlist := []User{
                {"a", 4},
                {"a", 2},
                {"b", 1},
                {"b", 3},
        }

        nameSorter := UserSorter{
                userlist,
                SortByNameAge,
        }

        ageSorter := UserSorter{
                userlist,
                SortByAge,
        }

        sort.Sort(UserSorter(nameSorter))
        fmt.Printf("%v\n", userlist)

        sort.Sort(UserSorter(ageSorter))
        fmt.Printf("%v\n", userlist)
}

function

func Float64s(a []float64)

Float64s sorts a slice of float64s in increasing order (not-a-number values are treated as less than other values).

func Float64sAreSorted(a []float64) bool

Float64sAreSorted tests whether a slice of float64s is sorted in increasing order (not-a-number values are treated as less than other values).

func Ints(a []int)

Ints sorts a slice of ints in increasing order.

func IntsAreSorted(a []int) bool

IntsAreSorted tests whether a slice of ints is sorted in increasing order.n

func IsSorted(data Interface) bool

func Search(n int, f func(int) bool) int

search 的 data 必须是已经排好序的

x := 23
i := sort.Search(len(data), func(i int) bool { return data[i] >= x })

if i < len(data) && data[i] == x {
     // x is present at data[i]
} else {
     // x is not present in data,
     // but i is the index where it would be inserted.
}

func SearchFloat64s(a []float64, x float64) int

func SearchInts(a []int, x int) int

func SearchStrings(a []string, x string) int

func Slice(slice interface{}, less func(i, j int) bool)

people := []struct {
     Name string
     Age  int
}{
     {"Gopher", 7},
     {"Alice", 55},
     {"Vera", 24},
     {"Bob", 75},
}
sort.Slice(people, func(i, j int) bool { return people[i].Name < people[j].Name })
fmt.Println("By name:", people) // By name: [{Alice 55} {Bob 75} {Gopher 7} {Vera 24}]

sort.Slice(people, func(i, j int) bool { return people[i].Age < people[j].Age })
fmt.Println("By age:", people)  // By age: [{Gopher 7} {Vera 24} {Alice 55} {Bob 75}]

func SliceIsSorted(slice interface{}, less func(i, j int) bool) bool

func SliceStable(slice interface{}, less func(i, j int) bool)

people := []struct {
     Name string
     Age  int
}{
     {"Alice", 25},
     {"Elizabeth", 75},
     {"Alice", 75},
     {"Bob", 75},
     {"Alice", 75},
     {"Bob", 25},
     {"Colin", 25},
     {"Elizabeth", 25},
}

// Sort by name, preserving original order
sort.SliceStable(people, func(i, j int) bool { return people[i].Name < people[j].Name })
fmt.Println("By name:", people)     // By name: [{Alice 25} {Alice 75} {Alice 75} {Bob 75} {Bob 25} {Colin 25} {Elizabeth 75} {Elizabeth 25}]

// Sort by age preserving name order
sort.SliceStable(people, func(i, j int) bool { return people[i].Age < people[j].Age })
fmt.Println("By age:", people) // By age: [{Alice 25} {Bob 25} {Colin 25} {Elizabeth 25} {Alice 75} {Alice 75} {Bob 75} {Elizabeth 75}]

func Sort(data Interface)

func Stable(data Interface)

func Strings(a []string)

func StringsAreSorted(a []string) bool

type Float64Slice

func (p Float64Slice) Len() int

func (p Float64Slice) Less(i, j int) bool

func (p Float64Slice) Search(x float64) int

func (p Float64Slice) Sort()

func (p Float64Slice) Swap(i, j int)

type IntSlice

func (p IntSlice) Len() int

func (p IntSlice) Less(i, j int) bool

func (p IntSlice) Search(x int) int

func (p IntSlice) Sort()

func (p IntSlice) Swap(i, j int)

type Interface

func Reverse(data Interface) Interface

type StringSlice

func (p StringSlice) Len() int

func (p StringSlice) Less(i, j int) bool

func (p StringSlice) Search(x string) int

func (p StringSlice) Sort()

func (p StringSlice) Swap(i, j int)