Sort Primitives (strings, ints, and floats) in Go

Jeff Roberts
InstructorJeff Roberts

Share this video with your friends

Send Tweet
Published 3 years ago
Updated a year ago

There are numerous ways to sort slices in Go.

The simplest way uses some basic built-in functions from Go's sort package to allow you to easily sort slices of primitives, that being Strings, Ints and Float64s. Using these sort functions, we don't have control over the sort order as the data will be sorted in ascending alphabetical (for strings) or ascending numerical (for Ints and Float64s) order.

In this lesson, we will take a quick look at the easiest way to sort slices of primitives. We will learn about using some of the handy built-in functions in Go's sort package, including:

  • sort.Strings: Sorting a slice of strings
  • sort.Ints: Sorting a slice of ints
  • sort.Float64s: Sorting a slice of float64s

Instructor: [0:00] In this lesson, we're going to learn how to sort slices of primitives. That would be strings, ints, and float64s. You can see here I start by loading some data. Let's take a look at what that looks like. This is some JSON data that I load from a file. In each piece of data is a TFT champion. Each champion contains a name, some classes, origins, and a cost.

[0:30] If we look at the data.go file here, you can see the struct that I'm loading this into that has corresponding fields for each of those JSON elements.

[0:41] If I go back here, the first thing I want to do is declare a slice of strings and enumerate through all of the loaded champions and extract the champion's name, and then take the first 10 elements, because I have so many champions in the file. I just want to work with some of them. I print this out.

[1:05] Let's run this. We'll see where we end up here. You can see here that the first 10 names of the champions, and they're in no particular order, you can see, it indicates that the data is not sorted. I'm illustrating usage of the first built-in sort function. This is in the Golang Sort package called StringsAreSorted.

[1:32] It will return a Boolean as to whether or not the provided slice of strings is sorted. Of course, it says false. Let's go ahead and sort this. I'm going to use a second built-in function in the sort package called strings, which accepts a slice of strings. What this will do is sort the strings in ascending alphabetical order.

[2:01] The next line here will simply log out what I've sorted and use the same StringsAreSorted function to report whether or not that slice it sorted. Let me go ahead and run that. Take a look at the results.

[2:18] In the second line here that I printed, shows that that function in fact sorted the names in alphabetical order and usage of that function reports that it is in fact sorted. I told you earlier, a Go has a simple way to sort primitives, that being strings, ints, and float64s.

[2:40] Let's take a look at how to do this with ints. Here, I create a slice of ints called gold. I iterate over all of my original list of loaded champions, which are quite a few. I'm appending the gold cost to this gold slice. Then I'm taking the first 10 elements out of there just to cut down on the data and logging out what we have.

[3:03] Let's run this. We can see I just get a bunch of ints in no particular order, which are the order of the first 10 champions. This is how much each of the first 10 champions costs. If you look, it says Sorted = false because Go has a similar IntsAreSorted function, which reports whether a slice of ints is sorted.

[3:26] You probably guessed how easy it is to sort the ints. Here, we used sort.strings passing a slice of strings. Go has a similar function called sort.ints, which takes a slice of ints, sorts the slice in place. It doesn't return anything just like the string version. Then I log out the results. If we take a look here, you can see that the final line that I logged sorted the ints in ascending order and reports that the ints are sorted.

[4:07] There's a similar function called sort.float64s. I'm not going to illustrate that, but it works exactly like strings and ints, and you can use it the same way.

Joseph Michael Casey
Joseph Michael Casey
~ 2 years ago

It looks like the link to your code for this lesson is not available.

https://github.com/JeffBNimble/GoLessons/tree/master/Array-Sort

Jeff Roberts
Jeff Robertsinstructor
~ 2 years ago

Joseph, I'm very sorry for this and thank you very much for catching this. The code for numerous sort lessons has been pushed to GitHub. I have a couple more sort lessons in the works, so watch for those.

Joseph Michael Casey
Joseph Michael Casey
~ 2 years ago

Hey Jeff,

How are you running your code? I tried go run sort_primitives.go to no avail. Any suggestions on running the API server you show in the video?

Jeff Roberts
Jeff Robertsinstructor
~ 2 years ago

Hi Joseph, I'm really sorry for the trouble. The feedback is very valuable. I will update instructions on the GitHub repo to make it easier to run. Until then, here is what you need to do.

The reason that it is not obvious is that this GitHub project actually supports multiple lessons. So, if you locate main.go, you'll notice that in the main func, there is currently a call to sortSlices(). If you look at the sort_slices.go and the sort_primitives.go, you'll see the code for the different lessons.

If you want to run the code for the Sort Primitives lesson, change the main func in main.go to call sortPrimitives(). If you want to run the code for the Sort Slices lesson, change the main func in main.go to call sortSlices().

Hopefully, that clears up the confusion. And again, thank you for the feedback. I will get these instructions updated in the project README.