AppDividend
Latest Code Tutorials

# Golang Sort Slice: How To Sort Int, Float64, String in Go

Golang sort Package provides primitives for sorting slices and user-defined collections. Golang’s sort package implements sorting for inbuilt and user-defined types. Sort methods are specific to the inbuilt type. Please note that sorting is in place, so it changes the given slice and doesn’t return a new one.

## Golang sort slice

Go sort.Ints() function sorts a slice of ints in increasing order. Golang sort.float64() function sorts a slice of float64s in increasing order (not-a-number values are treated as less than other values). Golang sort.Strings() function sorts a slice of strings in increasing order.

### Syntax

See the following syntax.

`func Ints(a []int)`

The Ints() function takes the slice of integers as a parameter.

### Example

```// hello.go

package main

import (
"fmt"
"sort"
)

func main() {

ints := []int{18, 21, 19}
sort.Ints(ints)
fmt.Println("Ints: ", ints)

s := sort.IntsAreSorted(ints)
fmt.Println("Sorted: ", s)
}
```

In the above code, first, we have defined the slice of integers and then pass that to the sort.Ints() function.

Then we have printed the sorted slice of integers.

We can also use sort to check if the slice is already in sorted order. We have used the sort.IntsAreSorted() function to check if the slice is sorted or not. Let’s see the output.

```go run hello.go
Ints:  [18 19 21]
Sorted:  true```

## Sort slice of float64 in Go

Golang sort.Float64s() is an inbuilt function that sorts a slice of float64s in increasing order (not-a-number values are treated as less than other values).

### Syntax

`func Float64s(a []float64)`

The Float64s() function takes the slice of the floats parameter.

### Example

```// hello.go

package main

import (
"fmt"
"sort"
)

func main() {

fldata := []float64{1.1, -21.19, 18.21, 46.11, -11.21}

sort.Float64s(fldata)
fmt.Println(fldata)

fl := sort.Float64sAreSorted(fldata)
fmt.Println("Sorted: ", fl)
}
```

#### Output

```go run hello.go
[-21.19 -11.21 1.1 18.21 46.11]
Sorted:  true```

In the above code, first, we have defined the slice of Float64 values. Then we have used the Float64s() function to sort the values of slice and then check if the values are sorted or not using the Float64sAreSorted() function.

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

## Sort slice of strings in Go

Golang sort.Strings() is an inbuilt function that sorts a slice of strings in increasing order.

### Syntax

`func Strings(a []string)`

### Example

```package main

import (
"fmt"
"sort"
)

func main() {

strs := []string{"The Stranger", "50 States of Fright", "Most Dangerous Game", "Flipped"}
sort.Strings(strs)
fmt.Println(strs)

s := sort.StringsAreSorted(strs)
fmt.Println("Sorted: ", s)
}
```

#### Output

```go run hello.go
[50 States of Fright Flipped Most Dangerous Game The Stranger]
Sorted:  true```

## Golang Sort with a custom comparator

Use the function sort.Slice() function. It sorts the slice using the provided function less(i, j int) bool. To sort a slice while keeping the original order of equal items, use sort.SliceStable() instead.

See the following code.

```package main

import (
"fmt"
"sort"
)

func main() {
quibi := []struct {
Name     string
Episodes int
}{
{"Most Dangerous Game", 8},
{"Survival", 7},
{"50 States of Fright", 9},
{"The Stranger", 5},
}

// Sort by episodes, keeping original order or equal elements.
sort.SliceStable(quibi, func(i, j int) bool {
return quibi[i].Episodes < quibi[j].Episodes
})
fmt.Println(quibi)
}
```

#### Output

```go run hello.go
[{The Stranger 5} {Survival 7} {Most Dangerous Game 8} {50 States of Fright 9}]```

In the above code, we have sorted the struct data type.

We can use the sort.Slicable() function and pass the struct and a function that sets the condition, and based on the condition, it will return the sorted output.

All algorithms in the Golang sort package make O(n log n) comparisons in the worst case, where n is the number of items to be sorted.

## Conclusion

To sort slices of basic types, use the sort package utilities. The sort functions for specific data types are defined under the sort package so, and you have to import sort package in your program and use the functions based on your requirement.

Finally, Sort Slice In Golang example is over.