我需要对来自3rdparty包的类型的切片进行排序。根据某些条件,顺序必须是升序或降序。

我想出的解决方案是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
type fooAscending []foo

func (v fooAscending) Len() int           { return len(v) }
func (v fooAscending) Swap(i, j int)      { v[i], v[j] = v[j], v[i] }
func (v fooAscending) Less(i, j int) bool { return v[i].Amount < v[j].Amount }

type fooDescending []foo

func (v fooDescending) Len() int           { return len(v) }
func (v fooDescending) Swap(i, j int)      { v[i], v[j] = v[j], v[i] }
func (v fooDescending) Less(i, j int) bool { return v[i].Amount > v[j].Amount }

if someCondition {
    sort.Sort(fooAscending(array))
} else {
    sort.Sort(fooDescending(array))
}

有没有更好的方法可以做到这一点。此任务的13行代码,其中大部分是重复的,似乎有点太多。

从Go 1.8开始,有一种对切片进行排序的简便方法,它不需要您定义新类型。您只需将匿名函数传递给sort.Slice函数。

1
2
3
4
5
6
7
a := []int{5, 3, 4, 7, 8, 9}
sort.Slice(a, func(i, j int) bool {
    return a[i] < a[j]
})
for _, v := range a {
    fmt.Println(v)
}

这将以升序排列,如果您想要相反的话,只需在匿名函数中编写a[i] > a[j]

  • 可悲的是它对int类型有限制
  • @LewisChan它不受int类型的限制; int参数是切片的索引,该切片可以是字符串的切片。

您正在寻找sort.Reverse。那会让你说:

1
sort.Sort(sort.Reverse(fooAscending(s)))

我在下面的回答基于一个假设,即您从第三方程序包中接收的切片是基本的Go类型。

要对基本类型的切片进行排序,请使用sort软件包实用程序。这是一个对字符串切片和int切片进行排序的示例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package main

import (
   "fmt"
   "sort"
)

func main() {
    sl := []string{"mumbai","london","tokyo","seattle"}
    sort.Sort(sort.StringSlice(sl))
    fmt.Println(sl)

    intSlice := []int{3,5,6,4,2,293,-34}
    sort.Sort(sort.IntSlice(intSlice))
    fmt.Println(intSlice)
}

上面的输出是:

1
2
[london mumbai seattle tokyo]
[-34 2 3 4 5 6 293]

转到此处的Playground尝试一下。

注意事项:

  • 排序基本的Go类型不需要实现属于sort.Interface的诸如Len()之类的函数。您只需要对复合类型采用该路由。

  • 只需使用适当的接口方法提供程序包装基本类型的类型,例如StringSlice,IntSlice或Float64Slice,然后排序。

  • 该切片是就地排序的,因此不会返回已排序切片的副本。

  • 1
    2
    3
    4
    5
    `var names = []string{"b","a","e","c","d"}
        sort.Strings(names)
        fmt.Println("Sorted in alphabetical order", names)
        sort.Sort(sort.Reverse(sort.StringSlice(names)))
        fmt.Println("Sorted in reverse order", names)`

    The Go Playgound的链接https://play.golang.org/p/Q8KY_JE__kx