Let's Learn Algorithms: Sorting a Custom User Type with Bubble Sort

This is the third and final practice problem for Bubble Sort in the Let's Learn Algorithms series.

In this post we are going to be covering the final practice problem introduced after we discussed how bubble sort works and implemented bubble sort in Go.

We are going to look at how to sort a list containing a custom type. Specifically, we are going to look at sorting a list with a User type where we want to ort by the user’s last name, but if that matches we want to fall back on the user’s first name.

Remember, if you want to stay up to date with new publications in this series I suggest you sign up for my mailing list.

 

If you sign up you will occasionally get emails from me letting you know about new posts I have published along with free content, like the first three chapters from my book - Web Development with Go. And if you don’t like it, you can unsubscribe at any time.

If you aren’t already familiar with bubble sort I suggest you check out the previous articles, and if you are unfamiliar with the practice problem and want to give it a try on your own you can see read about it here and you can find the code we will be starting with on github.

Sorting a list of users

Once again, the code we are going to use to get started is extremely similar to code we used in the last practice problem. I tried to intentionally build upon each problem so that you didn’t need to introduce too much new code in each section, which means a lot of copy/pasta is possible :)

The same thing is going to apply here. We are going to just copy all of the code from practice problem #2’s solution, and then update it to only work with the last name, and then finally update it to work with both the last and first name of a user.

Once you copy over the code, update all of the variables to use the User type instead of the string type and you may want to rename a few variables.

Lastly, update the greater() function to just return true all the time. We will fix it in a second, but right now we are just copying code and making sure everything compiles.

Your code should now look like the code here: https://play.golang.org/p/NN4RPqSQXj

Now that we have all of our code copied over, it should be pretty obvious what we need to do, but just in case it isn’t - we now need to implement the greater() function so that it works with the User type.

Remember in the last practice problem how we would convert strings to lower case and then compare them? Well, our code here is going to be pretty similar. We are going to compare the last name of each user after converting each name to lowercase.

func greater(a, b User) bool {
  if strings.ToLower(a.LastName) > strings.ToLower(b.LastName) {
    return true
  } else {
    return false
  }
}

This code gets us halfway to where we want to be, but it is missing one specific use case. What happens if the last names are the same?

In that case we want to sort by the first name, so our greater() function needs to take that into account by first checking if the two last names are equal.

When the last names are indeed equal, we need to compare the first names, but if the last names aren’t the same we never need to check the first names, because the last name always takes priority.

The code for this is shown below.

func greater(a, b User) bool {
  aLower := strings.ToLower(a.LastName)
  bLower := strings.ToLower(b.LastName)

  if aLower == bLower {
    // We want to know if a's first name is greater if last names match
    return strings.ToLower(a.FirstName) > strings.ToLower(b.FirstName)
  } else if aLower > bLower {
    return true
  } else {
    return false
  }
}

You can also find a runnable version of this code on the Go Playground, or you can find it in the Let’s Learn Algorithms GitHub repo.

In summary…

Want to stay up to date with new releases in the series? Sign up for my mailing list and I will notify you when I publish new articles. I will also send you the first three chapters from my upcoming book, Web Development with Go, and other exclusive content that I only send to my mailing list.

This was a relatively short writeup because it didn’t introduce a ton of new content, but I thought it was important to at least show you an example of how you can sort items using a fallback criteria, much like you would sort a real list of names.

Learn Web Development with Go!

Sign up for my mailing list and I'll send you a FREE sample from my course - Web Development with Go. The sample includes 19 screencasts and the first few chapters from the book.

You will also receive emails from me about Go coding techniques, upcoming courses (including FREE ones), and course discounts.

Avatar of Jon Calhoun
Written by
Jon Calhoun

Jon Calhoun is a full stack web developer who teaches about Go, web development, algorithms, and anything programming. If you haven't already, you should totally check out his Go courses.

Previously, Jon worked at several statups including co-founding EasyPost, a shipping API used by several fortune 500 companies. Prior to that Jon worked at Google, competed at world finals in programming competitions, and has been programming since he was a child.

Related articles

Spread the word

Did you find this page helpful? Let others know about it!

Sharing helps me continue to create both free and premium Go resources.

Want to discuss the article?

See something that is wrong, think this article could be improved, or just want to say thanks? I'd love to hear what you have to say!

You can reach me via email or via twitter.

Recent Articles All Articles Mini-Series Progress Updates Tags About Me Go Courses

©2018 Jonathan Calhoun. All rights reserved.