Go - sorting slices

Go - sorting slices

In Go, sorting a slice is often necessary when working with data that needs to be organized or structured for better usability, efficiency, or presentation. Here are some common scenarios where you might need to sort a slice:

Displaying Sorted Data Improving Search Efficiency Custom Ranking or Prioritization Data Cleaning and Organization

SortFunc function

In order to sort a slice we can use the slices.SortFunc function. The slices.SortFunc function receives a slice and a comp function in order to sort the values. This comp function should return -1 if a < b, 1 if a > b and 0 if the values are equals.

var mySlice []string

mySlice = append(mySlice, "A")
mySlice = append(mySlice, "B")
mySlice = append(mySlice, "C")
mySlice = append(mySlice, "Z")
mySlice = append(mySlice, "M")
mySlice = append(mySlice, "Y")
mySlice = append(mySlice, "P")

slices.SortFunc(mySlice, func(a, b string) int {
    return strings.Compare(a, b)
})

for _, v := range mySlice {
    fmt.Println(v)
}        
>>> A
>>> B
>>> C
>>> M
>>> P
>>> Y
>>> Z        

If we would like to sort a slice of integer, we could change the function this way:

var mySlice []int

mySlice = append(mySlice, 55)
mySlice = append(mySlice, 53)
mySlice = append(mySlice, 105)
mySlice = append(mySlice, 3)
mySlice = append(mySlice, 375)
mySlice = append(mySlice, 376)
mySlice = append(mySlice, 377)
mySlice = append(mySlice, 250)
mySlice = append(mySlice, 15)

slices.SortFunc(mySlice, func(a, b int) int {
    return a - b
})

for _, v := range mySlice {
    fmt.Println(v)
}        
>>> 3
>>> 15
>>> 53
>>> 55
>>> 105
>>> 250
>>> 375
>>> 376
>>> 377        

Sorting a map

If we need to sort a map, we can create a struct in order to hold the values of the map, and then we can sort a slice of this struct.

package main

import (
    "fmt"
    "slices"
    "strings"
)

func main() {
    fmt.Println("Starting")

    var myMap = map[string]int{
        "G": 7,
        "B": 2,
        "C": 3,
        "E": 5,
        "A": 1,
        "D": 4,
        "H": 8,
        "F": 6,
    }

    type kv struct {
        Key   string
        Value int
    }

    var kvSlice []kv
    for k, v := range myMap {
        kvSlice = append(kvSlice, kv{Key: k, Value: v})
    }

    for k, v := range kvSlice {
        fmt.Println(k, v)
    }

    fmt.Println("----------")

    slices.SortFunc(kvSlice, func(a, b kv) int {
        return strings.Compare(a.Key, b.Key)
    })

    for k, v := range kvSlice {
        fmt.Println(k, v)
    }

}        
>>> 55
>>> 53
>>> 105
>>> 3
>>> 375
>>> 376
>>> 377
>>> 250
>>> 15
>>> ----------
>>> 3
>>> 15
>>> 53
>>> 55
>>> 105
>>> 250
>>> 375
>>> 376
>>> 377        


要查看或添加评论,请登录

Daniel M.的更多文章

  • Golang HTTP Requests - Complete Guide

    Golang HTTP Requests - Complete Guide

    HTTP Requests In Go, we can use the net/http package to make http requests. We will use this public endpoint to make a…

  • Using structs with Redis and Golang

    Using structs with Redis and Golang

    Many times, we will need to store a struct in Redis instead of storing only separate fields. We can store a struct like…

  • Redis Hash

    Redis Hash

    A Redis Hash is a data structure in Redis that stores a set of key-value pairs, similar to an object or map in…

  • Go Nested Templates

    Go Nested Templates

    We can nest templates in Go, allowing us to create smaller parts of a template, similar to components. In this example,…

  • Go Templates 02 - Conditionals

    Go Templates 02 - Conditionals

    Conditional Templates Another very common feature we need when working with templates is the use of conditions. For all…

  • Go Templates 01 - Basics

    Go Templates 01 - Basics

    Templates Go provides a powerful template system through the text/template and html/template packages, enabling the…

  • Go - Arrays

    Go - Arrays

    Arrays Array is a data structure used to store a sequence of elements of same type in a fixed-size. An array is stored…

  • Redis SET command.

    Redis SET command.

    Redis set command SET We can set a string value to a key using the set command. In order to check if the value was…

  • Golang - Recursion

    Golang - Recursion

    Recursion The programming language Golang is known for its simplicity, efficiency and for work really well with…

社区洞察

其他会员也浏览了