Cookies management by TermsFeed Cookie Consent
Russia has invaded Ukraine and already killed tens of thousands of civilians, with many more raped or tortured. It's a genocide. We need your help. Let's fight back against the Russian regime.
Help Ukraine! Fight the Russian regime!

◀️ Reverse sort a slice in Go

sort slice

Please consider supporting us by disabling your ad blocker

In Go, if you have a slice of simple type like []int, []string, or []float64, you can sort the slice using the sort.Ints(), sort.Strings(), or sort.Float64s() functions. However, they only sort a slice in ascending order. To sort in descending order, you must use the more general function sort.Sort() with the data reversal function sort.Reverse().

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

import (
    "fmt"
    "sort"
)

func main() {
    numbers := []int{4, 3, 2, 1, 0, 4, 7, 5}
    // sort ints ascending
    sort.Ints(numbers)
    fmt.Println(numbers)

    // sort ints descending
    sort.Sort(sort.Reverse(sort.IntSlice(numbers)))
    fmt.Println(numbers)

    // sort strings descending
    chars := []string{"a", "b", "c"}
    sort.Sort(sort.Reverse(sort.StringSlice(chars)))
    fmt.Println(chars)
}

Output:

[0 1 2 3 4 4 5 7]
[7 5 4 4 3 2 1 0]
[c b a]

Look at the example. To sort a []int slice in descending order you need to:

  1. Convert the []int slice to sort.IntSlice, which makes the slice an instance of the sort.Interface interface.
  2. Reverse the standard ascending order of the elements included in the sort.IntSlice by using the sort.Reverse() function.
  3. Sort the reversed slice using the general sort.Sort() function.

The same scheme applies when sorting a []string or []float64 list, but it must be converted to sort.StringSlice or sort.Float64Slice.

There is no built-in option to reverse the order when using the sort.Slice() with a custom sorting function less. However, we can do it ourselves. In the example below, the reverse() function “reverses” the less function. This way, we can create a single less function for sorting a slice of structs and then sort it in ascending or descending order depending on a parameter or use case.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main

import (
    "fmt"
    "sort"
)

type Fruit struct {
    Name  string
    Color string
    Rank  int
}

func reverse(less func(i, j int) bool) func(i, j int) bool {
    return func(i, j int) bool {
        return !less(i, j)
    }
}

func main() {
    fruits := []Fruit{
        {
            Name:  "Strawberry",
            Color: "red",
            Rank:  2,
        },
        {
            Name:  "Raspberry",
            Color: "pink",
            Rank:  1,
        },
        {
            Name:  "Banana",
            Color: "yellow",
            Rank:  3,
        },
    }

    less := func(i, j int) bool {
        return fruits[i].Rank < fruits[j].Rank
    }

    // sort structs ascending
    sort.Slice(fruits, less)
    fmt.Println(fruits)

    // sort structs descending
    sort.Slice(fruits, reverse(less))
    fmt.Println(fruits)
}

Output:

[{Raspberry pink 1} {Strawberry red 2} {Banana yellow 3}]
[{Banana yellow 3} {Strawberry red 2} {Raspberry pink 1}]

🧮 Sort a string slice containing numbers in Go

Learn how to sort strings with numbers in natural order
sort slice strings

🗑️ Remove duplicates from any slice using Generics in Go

Learn how to create a slice with unique values using Generics
introduction slice generics generics-intro

✂️ Remove duplicates from a slice in Go

Learn how to create a slice with unique values
introduction slice