Sorting Workout in Golang
Sorting the list of data is one of the most common tasks you may find yourself coding ever so often. This article explores a few techniques in Golang that should make the sorting of any data collection simple.

All programming languages often provide ready utils to sort collections due to the incredibly common nature of this task in solving coding assignments. Go is no different with its provision of the sort package for performing all possible sorting operations.
Note: Before you go any further, I expect you all to have a beginner-level understanding of Go syntax and primitive types to understand the source code.
Go has the standard sort
package for doing the sorting. The sorting functions sort data in place. For basic data types, we have built-in functions such as sort.Ints
and sort.Strings
. For more complex types, we need to create our own sorting by implementing the sort.Interface
. Golang’s sort
the package implements sorting for builtins and user-defined types.
Sort a collection of integers
The sort.Ints
function sorts a slice of integers in ascending order.
The sort.IntsAreSorted
the method also exists to check if an integer slice is in its sorted form or not.
Sort a collection of strings
The sort.Strings
function sorts a slice of strings in ascending order.
The sort.StringsAreSorted
the method also exists to check if a string slice is in its sorted form or not.
Sort a collection of floats
The sort.Float64s
function sorts a slice of floating-point values in ascending order.
math.NaN()
returns an IEEE 754 “not-a-number” value. Float64sAreSorted()
reports whether the slice x is sorted in increasing order, with not-a-number (NaN) values before any other values.
Sort a collection of structs using an anonymous function
To sort a slice of structs in Golang, you need to use a less
function along with either the sort.Slice
or sort.SliceStable
methods. You need to provide the implementation of less
functions to compare structure fields. Here’s how we can sort a slice of persons according to their names and ages for example:
Returning true
from the less
function will cause the element at the index i
to be sorted to a lower position than the index j
(The element at index i
will come first in the sorted slice). Otherwise, the element at the index j
will come first if false
is returned.
The difference between sort.Slice
and sort.SliceStable
is that the latter will keep the original order of equal elements while the former may not.
Sort custom data structures with Len, Less, and Swap
For more complex types, we need to create our own sorting by implementing the sort.Interface
:
type Interface interface {
Len() int
Less(i, j int) bool
Swap(i, j int)
}
The below example illustrates this type of implementation.
Original List — [“rust-lang”, “golang”, “java”, “c”, “c++”, “dot-net”, “perl”]
Sorted List — [“c”, “c++ ”,“perl”, “java”, “golang”, “dot-net ”, “rust-lang”]
A new type LengthBasedStrings is defined to hold the list of strings in it. This type implements the required interface, hence it is easily used with sort.Sort() method. The definition of the Sort() method is
func sort.Sort(data sort.Interface)
Sort maps keys or values
Maps are unordered collections in Golang so there is no provision for sorting a map using the sort
package. However, if you really need to sort a map, you can write your custom implementation for it by slicing out keys & values separately.
Conclusion
Sorting collections of data are pretty straightforward in Golang through the use of the sort
package and the solutions presented above should suffice for the majority of situations you will face.
Thanks for reading, and happy coding! 😃