Go Pattern: Sorting a slice on multiple keys

Hi 👋 In this article I want to highlight a simple pattern for sorting a slice in Go on multiple keys. Given the following structure, let’s say we want to sort it in ascending order after Version, Generation and Time. 1 2 3 4 5 type TheStruct struct { Generation int Time int Version int } The way we sort slices in Go is by using the sort interface or one of the sort.Slice functions. To sort the slice after the above criteria we’ll call slice.Sort with the following function. ...

May 12, 2022 · 3 min · Denis Nuțiu