sort a list in go in go

To sort a list in Go, you can use the sort package provided by the standard library. The sort package works with slices, which are dynamic arrays in Go.

Here's an example of how to sort a list of numbers in ascending order using the sort package:

main.go
package main

import (
    "fmt"
    "sort"
)

func main() {
    nums := []int{4, 2, 7, 1, 3, 6, 5}

    // Use the sort.Ints() function to sort the slice in ascending order.
    sort.Ints(nums)

    fmt.Println(nums)  // Output: [1 2 3 4 5 6 7]
}
248 chars
16 lines

In this example, we first define a slice containing a list of numbers. We then use sort.Ints() to sort the slice in ascending order. Finally, we print the sorted slice to the console. You can use similar functions like sort.Strings() to sort other types of slices.

related categories

gistlibby LogSnag