How to Sort Strings With Go Alphabetically in Any Language
2 min read Go Programming

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

A

Alex

Passionate about web development and sharing knowledge with the community.

Share on X

You might also like

How to test database interactions in golang applications
2 min read

How to test database interactions in golang applications

Testing of functions with database interactions always was challenging. Recently, I found a wonderful library, which will simplify writing tests and mocking database queries in golang applications a lot. And I want to share a short example of how to work with it.

Read more