Master the Basics: How to Sort in Go with Ease


GO

Let me take you through sorting in Go, from simple sorting of built-in types like integers and strings to advanced techniques for custom sorting with the sort package. We will explore how to implement custom sorting logic, stable vs unstable sorting and even sorting of maps. At the same time, together we shall also delve into utility functions and performance considerations to have a complete knowledge about sorting in Go.

1. Understanding the Golang sort Package

The sort package in Go is a very powerful tool which offers numerous functions and types for sorting slices and user-defined collections. It is part of the standard library for Go which implies that it is always available for use without having to obtain external packages. Understanding how this package can be used will greatly enhance your Go programs especially when organizing or manipulating data.

1.1 Primary Sorting Functions

The sort package has these few prebuilt functions meant specifically for directly sorting slices containing built-in types:

  • sort.Ints: This function sorts a slice containing integers in ascending order.
  • sort.Float64s: It sorts float64s slice in ascending order.
  • sort.Strings: The function sorts strings slice in ascending order.

These functions are convenience based, resolving most common needs for which minimal effort was invested.

1.2 The sort.Interface Type

To meet more sophisticated sorting needs such as those applied on slices of custom structs, the sort package defines the sort.Interface. Three methods are required by this interface namely; Len(), Less(i int, j int) bool and Swap(i int, j int). By defining these methods any slice can be sorted using the sort.Sort function.

1.3 Custom Sorting and Utility Functions

Apart from basic purposeful ordering, there are other useful functionalities proffered within the kindred context of custom-made sorts by the package:

  • sort.Slice and sort.SliceStable: The two allow us to create a sorted list without following all that requirement of implementing our type as a sort.Interface. For instance while using sort.SliceStable one is assured that if two elements are same then their original order in the input will be the same as their output order.
  • sort.IsSorted: The function checks whether a slice is sorted based on sort.Interface.
  • sort.Search: This gives you a general purpose binary search to look for an element in a sorted array, but requires either implementing the sort.Interface or using a compatible comparison function.

 

2. Sorting Built-in Types in Go

In Go, the sorting of built-in types like integer slices, string and float is simple and clear due to the handy packages offered by the sort package. These functions are created to make the process of sorting as quick and easy as possible without having to write a custom sorting algorithm.

 

2.1 Sorting Slices of Integers

To sort an integer slice, you can use sort.Ints(). It takes a slice of integers ([]int) as input argument and sorts it in place in ascending order. sort.Ints does not return anything since it works directly on the provided slice.

nums := []int{3, 1, 4, 1}
sort.Ints(nums)
fmt.Println(nums) // Output: [1, 1, 3, 4]

 

2.2 Sorting Slices of Strings

For sorting a slice containing strings, one uses the function sort.Strings(). Like sort.Ints(), this function takes a slice of strings ([]string) and sorts it in place into increasing lexicographical order (in dictionary order or most English purposes).

strs := []string{"peach", "banana", "kiwi"}
sort.Strings(strs)
fmt.Println(strs) // Output: [banana, kiwi, peach]

 

2.3 Sorting Slices of Floats

When slicing floating point numbers, prefer using sort.Float64s. It arranges a float64 slices ([]float64) in ascending order. Sorting happens inside this function just like for other sorting functions.

floats := []float64{2.718, 3.142, 1.414, 1.732}
sort.Float64s(floats)
fmt.Println(floats) // Output: [1.414, 1.732, 2.718, 3.142]

 

2.4 How to sort in GO in Reverse order

Sometimes, you might need the data in descending order. While there isn't a direct function like sort.ReverseInts(), Go offers a convenient way to reverse sort using the sort.Reverse() function combined with the basic sort functions.

nums := []int{45, 12, 89, 3, 56}
sort.Sort(sort.Reverse(sort.IntSlice(nums)))
fmt.Println(nums)  // Output: [89 56 45 12 3]

This technique can be similarly applied to slices of float64 and string by using sort.Float64Slice and sort.StringSlice respectively.

NOTE:
These functions modify the original slice; they do not return a new slice.

 

3. Implementing Custom Sorting

 There are basically two main ways of implementing custom sorting in Go.

Firstly, it is through using sort.Interface to define a custom sorting logic for any type or secondly, we may use the convenience functions sort.Slice and sort.SliceStable for simpler cases. Below is how each one works:

 

3.1 Custom Sorting with sort.Interface

In this case, we create a custom order for a slice of user defined type by implementing the sort.Interface on that type. The interface has three methods as follows: Len(), Less(i, j int) bool and Swap(i, j int).

  • Len() Method: This returns the number of elements contained in the collection to be sorted.
  • Less(i, j int) bool Method: It determines the order of elements at index i and j. It returns true if the element at i should sort before the element at j.
  • Swap(i, j int) Method: Elements with indexes i and j will be interchanged.

When you implement these methods, what you are doing is defining how your slice should be sorted. Here’s an example where we can easily see how one can go about sorting a slice of a custom struct type by one of its fields:

type Person struct {
    Name string
    Age  int
}

// ByAge implements sort.Interface for []Person based on
// the Age field.
type ByAge []Person

func (a ByAge) Len() int           { return len(a) }
func (a ByAge) Less(i, j int) bool { return a[i].Age < a[j].Age }
func (a ByAge) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Usage
people := []Person{{"Bob", 31}, {"Alice", 22}, {"Eve", 26}}
sort.Sort(ByAge(people))

 

3.2 Simplified Custom Sorting

If creating your own type and implementing sort.Interface seems like overkill then Go provides two handy functions namely; sort.Slice and sort.SliceStable.

  • sort.Slice: The slice gets sorted according to less function which you define inline. This is useful in quick sorts where a separate type together with its methods would otherwise not be necessary.
  • sort.SliceStable: Like sort.Slice but with guaranteed retention of original order where equal elements are involved hence providing stable sorting.

Below is an example of using it on a list of Person structs without having to implement the sort.Interface:

people := []Person{{"Bob", 31}, {"Alice", 22}, {"Eve", 26}}
sort.Slice(people, func(i, j int) bool { return people[i].Age < people[j].Age })

Similarly if it was required that people with same age retain their original positions after being ordered they would still call upon state.SortSliceStable() in the same way.

When deciding whether to use sort.Slice or sort.SliceStable, you’ll want to consider if the order of equal elements needs to be preserved for your specific application.

 

4. Stable vs. Unstable Sorting

In sorting, whether a sorting algorithm keeps the relative order of equal keys (values) or not is called stable or unstable sorting respectively. It is important to understand the difference between stable and unstable sorting for some cases where the order of equal elements matters.

 

4.1 Stable Sorting

A stable sorting algorithm maintains relative ordering of elements with same key as that in the input sequence. Go’s sort package provides this functionality through the sort.Stable function. One application area is when you have a slice already grouped or ordered based on one criterion and now you want to re-order them using another criterion without breaking up those groups.

Example: Imagine you have a slice of Employee records, and you initially sort them by their department. Later, you want to sort these employees by their last name while keeping the original ordering within each department. This is where stable sorting is essential.

type Employee struct {
    LastName  string
    Department string
}

employees := []Employee{
    {"Smith", "Accounting"},
    {"Jones", "Engineering"},
    {"Anderson", "Engineering"},
    {"Lewis", "Accounting"},
}

// First, sort by department (potentially using sort.Sort or sort.Slice)
sort.Slice(employees, func(i, j int) bool {
    return employees[i].Department < employees[j].Department
})

// Then, sort by last name within each department using sort.Stable
sort.Stable(sort.SliceStable(employees, func(i, j int) bool {
    return employees[i].LastName < employees[j].LastName
}))

// This ensures that within each department, employees are sorted by last name,
// but the department grouping remains intact as per the initial sort.

 

4.2 Unstable Sorting

Unstable sorting algorithms do not guarantee that the order of equal elements will be preserved. This could be a non-issue in situations where only the sorted order is important, irrespective of the initial locations of equal elements. For such cases you can consider using functions like sort.Sort that do not require you to preserve ordering of equal items.

Example: For basic list of integers where we do not mind if some numbers come before others while others come after each other, an unstable sorting algorithm would suffice.

numbers := []int{4, 2, 3, 1, 4, 2}
sort.Ints(numbers) // Unstable sort is fine here as we don't have equal "keys" to worry about
fmt.Println(numbers)
// Output: [1, 2, 2, 3, 4, 4]

 

4.3 Key Differences

In the first example, sort.Stable is used to ensure that within each department, the employees' last names are sorted without disrupting the initial departmental ordering. This brings out an ideal situation for stable sorting as we have a second criterion which should not change how things were arranged in terms of departments during our first sort exercise.

In the second case we used an unstable sort named sort.Ints. In contrast to that it becomes possible to use an unstable sorter since there isn’t any other condition which calls for preservation (like groupings by departments as seen in example one).

 

Frequently Asked Questions

Why is it important to sort in programming?

It sorts the data into a particular order to make searching, analysis and visualization easier. The standard sort package in Go provides powerful algorithms for sorting different types of data.

How can I use Go to sort a slice of integers?

To do so, you will have to utilize the function called sort.Ints(). For example, if you are given a slice nums := []int{3, 1, 4}, then you can as well sort it by doing: sort.Ints(nums).

Is it possible to use the standard library for sorting in descending order?

Yes, you can use sort.Reverse() along with other sorting functions. For a slice of integers, sort.Sort(sort.Reverse(sort.IntSlice(nums))) will sort it in descending order.

How do I sort custom types or structs?

Firstly define the structure that contains three methods: Len(), Less() and Swap(); which implement the interface of sort.Interface. Then use these arrays with your custom type to invoke sort.Sort() method.

What is the difference between sort.Sort() and sort.Stable()?

Both functions are employed to organize slices but while functioning on equal elements’ original arrangement invariant is preserved by sort.Stable( ), it may not be so for sort.Sort( ).

How can I sort strings?

This is done through applying the function known as ‘sort.Strings(). Having any string such as strs := []string{"banana", "apple"}, all one has to do is calling this function for them.

How do I search within a sorted slice?

You need both the length of your slice and its ordering via an ordering function that tells whether one item is less than another item. This returns where an element would be inserted into that ordered list.

When sorting slices what pitfalls should one look out for?

Yes. Since a slice is merely a reference to an array, when sorting it, the original data gets modified too. Moreover, incorrect methods of sort.Interface will lead to unexpected results.

 

Conclusion

Sorting is an essential operation in programming, and Go offers a comprehensive standard package to facilitate this. Throughout this guide, we've covered the basic and advanced functionalities of the sort package. We've learned how to sort built-in types, custom data structures, and even explored more complex topics like stable and parallel sorting. With the powerful capabilities that Go offers for sorting, developers can efficiently manage and process their data in desired orders. By understanding these concepts, one can ensure data consistency, optimize search operations, and make data processing more predictable.

 

Views: 26
Deepak Prasad

Deepak Prasad

He is the founder of GoLinuxCloud and brings over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can connect with him on his LinkedIn profile.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment