How to Sort Strings With Go Alphabetically in Any Language
4 Jan 2021

How to Sort Strings With Go Alphabetically in Any Language

In this article I’m going to show how easy we can sort strings alphabetically in different languages, using Go. It seems like an easy task if we want to sort English words, however, it’s not so trivial if we want to sort correctly strings with special characters or in other languages, i.e Cyrillic based.

Let’s check this example of cities list:

  cities := []string{
    "Berlin",
    "Zurich",
    "Augsburg",
    "Bünde",
    "Budapest",
    "Ürkmez",
    "Rostock",
    "Ulm",
    "Lindau",
  }

If we use the standard way to sort strings with sort.Strings(cities) the result will be:

  [Augsburg Berlin Budapest Bünde Lindau Rostock Ulm Zurich Ürkmez]

As you can notice, Ürkmez ended up at the end of the list. And that’s not the correct order. Fortunately, Go has a powerful library golang.org/x/text/collate which could help us!

Package collate contains types for comparing and sorting Unicode strings according to a given collation order.

Let’s try to use it!

   c := collate.New(language.German, collate.IgnoreCase)
   c.SortStrings(cities)
   fmt.Println(cities)

It will print this result:

   [Augsburg Berlin Budapest Bünde Lindau Rostock Ulm Ürkmez Zurich]

Looks awesome! Right?

But what if you don’t sure in with language string was written? In this case, we can just use language.Und. Let’s check on this example:

  mixedLanguagesCities := []string{
    "Ürkmez",
    "Budapest",
    "Бохольт",
    "Арнсберг",
    "Інцель",
    "Їндржихув-Градец",
    "Єна",
    "Шатору",
    "Ястшембя-Ґура",
    "Ґрудзьондз",
    "Атланта",
    "Zurich",
  }

  c = collate.New(language.Und, collate.IgnoreCase)
  c.SortStrings(mixedLanguagesCities)
  fmt.Println(mixedLanguagesCities)

The result will be:

  [Budapest Ürkmez Zurich Арнсберг Атланта Бохольт Ґрудзьондз Єна Інцель Їндржихув-Градец Шатору Ястшембя-Ґура]

As you can see, it sorted correctly the mixed list of German and Ukrainian strings, according to official sorting rules.

That was it! Hope this information was helpful for you 😊

Example code you can find here

Related posts

Ristretto - the Most Performant Concurrent Cache Library for Go
2 Mar 2021

Ristretto - the Most Performant Concurrent Cache Library for Go

Recently, I discovered a surprisingly reliable memory caching solution, which I’m planning to use in all my further applications to increase performance. In this blog post, I will share some code examples of how you can integrate Ristretto caching library into your application.

go caching ristretto performance
How to Show Flash Messages in Go web applications (with Echo framework)
4 Feb 2021

How to Show Flash Messages in Go web applications (with Echo framework)

When we create a web application, usually, there a need to communicate with the users to inform them about the results of their actions. The easiest way to communicate - is to send messages. These messages might be warnings, errors, or just informational text. In this article, we will improve the UX of our user authentication application from the previous article by adding an error flash message when the user entered a wrong password and a success message after user authorisation.

go echo flash messages
User Authentication in Go Echo with JWT
28 Jan 2021

User Authentication in Go Echo with JWT

In this article, we will build a simple user authentication functionality using JWT (JSON Web Token). In the examples, I’m going to use a Go Echo framework. This will allow us to avoid writing some boilerplate code.

go authentication JWT Echo
A Simple Queue Implementation in Golang with channels
12 Jan 2021

A Simple Queue Implementation in Golang with channels

In this post, I’m going to show the way how we can implement a simple queue in Golang, using channels.

go queue channel goroutine
How to Control Router Access Permissions in Go Web Apps
23 Dec 2020

How to Control Router Access Permissions in Go Web Apps

In this post I’m going to describe how can we limit user access to the specific url in golang web application. I will use chi router - a lightweight, idiomatic and composable router for building Go HTTP services.

go router chi