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

One of The Easiest Ways to Host your Go Web App
5 Sep 2023

One of The Easiest Ways to Host your Go Web App

Hello! In this post, I will explain the cost-effective method I use to host my Go web applications with varying levels of complexity, all starting from as low as $5 per month. This method also allows to easy deploy and scale your golang application.

go hosting digitalocean docker
103 Early Hints in Go, or the new Way of How to Improve Performance of a Web Page written in Go
14 Nov 2022

103 Early Hints in Go, or the new Way of How to Improve Performance of a Web Page written in Go

Since Go 1.19 we can use a new 103 (Early Hints) http status code when we create web applications. Let’s figure out how and when this could help us. We are going to create a simple golang web server that servers some html content. One html page will be served with 103 header and another one without. After loading comparison we will see how early hints can improve page performance.

go golang performance
Example of how Golang generics minimize the amount of code you need to write
9 Jun 2022

Example of how Golang generics minimize the amount of code you need to write

I guess that almost everyone in the go community was exciting when Go 1.18 was released, especially because of generics. Some days ago I decided to try generics in the real-world application, by refactoring some of its pieces, related to a caching logic.

go generics redis cache
Concurrent Map Writing and Reading in Go, or how to deal with the data races.
16 Jul 2021

Concurrent Map Writing and Reading in Go, or how to deal with the data races.

This time, I will show you how to work with the maps in go effectively and prevent the occurrence of the data race errors. Data races happen when several goroutines access the same resource concurrently and at least one of the accesses is a write.

go concurrent map data race
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