<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Strings on WebDevStation</title>
    <link>https://webdevstation.com/tags/strings/</link>
    <description>1 article tagged Strings — tutorials, code examples and notes from building real systems, newest first.</description>
    <generator>Hugo</generator>
    <language>en</language>
    <managingEditor>Alex</managingEditor>
    <webMaster>Alex</webMaster>
    <copyright>© 2026 WebDevStation</copyright>
    <lastBuildDate>Mon, 04 Jan 2021 15:59:02 +0000</lastBuildDate>
    <atom:link href="https://webdevstation.com/tags/strings/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>How to Sort Strings With Go Alphabetically in Any Language</title>
      <link>https://webdevstation.com/posts/how-to-sort-strings-with-go-alphabetically-in-any-language/</link>
      <pubDate>Mon, 04 Jan 2021 15:59:02 +0000</pubDate>
      <author>Alex</author>
      <guid isPermaLink="true">https://webdevstation.com/posts/how-to-sort-strings-with-go-alphabetically-in-any-language/</guid>
      <description>Learn how to properly sort strings in Go across different languages, including handling special characters and non-Latin alphabets like Cyrillic using Go&#39;s…</description>
      <content:encoded><![CDATA[<p>In this article I&rsquo;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&rsquo;s not so trivial if we want to sort correctly strings with special characters or in other languages, i.e Cyrillic based.</p>
<p>Let&rsquo;s check this example of cities list:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-go" data-lang="go"><span style="display:flex;"><span>  <span style="color:#a6e22e">cities</span> <span style="color:#f92672">:=</span> []<span style="color:#66d9ef">string</span>{
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Berlin&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Zurich&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Augsburg&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Bünde&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Budapest&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Ürkmez&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Rostock&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Ulm&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Lindau&#34;</span>,
</span></span><span style="display:flex;"><span>  }
</span></span></code></pre></div><p>If we use the standard way to sort strings with <code>sort.Strings(cities)</code> the result will be:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>  <span style="color:#f92672">[</span>Augsburg Berlin Budapest Bünde Lindau Rostock Ulm Zurich Ürkmez<span style="color:#f92672">]</span>
</span></span></code></pre></div><p>As you can notice, <code>Ürkmez</code> ended up at the end of the list. And that&rsquo;s not the correct order.
Fortunately, Go has a powerful library <a href="https://pkg.go.dev/golang.org/x/text/collate">golang.org/x/text/collate</a> which could help us!</p>
<blockquote>
<p>Package collate contains types for comparing and sorting Unicode strings according to a given collation order.</p>
</blockquote>
<p>Let&rsquo;s try to use it!</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-go" data-lang="go"><span style="display:flex;"><span>   <span style="color:#a6e22e">c</span> <span style="color:#f92672">:=</span> <span style="color:#a6e22e">collate</span>.<span style="color:#a6e22e">New</span>(<span style="color:#a6e22e">language</span>.<span style="color:#a6e22e">German</span>, <span style="color:#a6e22e">collate</span>.<span style="color:#a6e22e">IgnoreCase</span>)
</span></span><span style="display:flex;"><span>   <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">SortStrings</span>(<span style="color:#a6e22e">cities</span>)
</span></span><span style="display:flex;"><span>   <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">cities</span>)
</span></span></code></pre></div><p>It will print this result:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>   <span style="color:#f92672">[</span>Augsburg Berlin Budapest Bünde Lindau Rostock Ulm Ürkmez Zurich<span style="color:#f92672">]</span>
</span></span></code></pre></div><p>Looks awesome! Right?</p>
<p>But what if you don&rsquo;t sure in with language string was written? In this case, we can just use <code>language.Und</code>. Let&rsquo;s check on this example:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-go" data-lang="go"><span style="display:flex;"><span>  <span style="color:#a6e22e">mixedLanguagesCities</span> <span style="color:#f92672">:=</span> []<span style="color:#66d9ef">string</span>{
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Ürkmez&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Budapest&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Бохольт&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Арнсберг&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Інцель&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Їндржихув-Градец&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Єна&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Шатору&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Ястшембя-Ґура&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Ґрудзьондз&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Атланта&#34;</span>,
</span></span><span style="display:flex;"><span>    <span style="color:#e6db74">&#34;Zurich&#34;</span>,
</span></span><span style="display:flex;"><span>  }
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">c</span> = <span style="color:#a6e22e">collate</span>.<span style="color:#a6e22e">New</span>(<span style="color:#a6e22e">language</span>.<span style="color:#a6e22e">Und</span>, <span style="color:#a6e22e">collate</span>.<span style="color:#a6e22e">IgnoreCase</span>)
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">c</span>.<span style="color:#a6e22e">SortStrings</span>(<span style="color:#a6e22e">mixedLanguagesCities</span>)
</span></span><span style="display:flex;"><span>  <span style="color:#a6e22e">fmt</span>.<span style="color:#a6e22e">Println</span>(<span style="color:#a6e22e">mixedLanguagesCities</span>)
</span></span></code></pre></div><p>The result will be:</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;-webkit-text-size-adjust:none;"><code class="language-shell" data-lang="shell"><span style="display:flex;"><span>  <span style="color:#f92672">[</span>Budapest Ürkmez Zurich Арнсберг Атланта Бохольт Ґрудзьондз Єна Інцель Їндржихув-Градец Шатору Ястшембя-Ґура<span style="color:#f92672">]</span>
</span></span></code></pre></div><p>As you can see, it sorted correctly the mixed list of German and Ukrainian strings, according to official sorting rules.</p>
<p>That was it! Hope this information was helpful for you 😊</p>
<p>Example code you can find <a href="https://github.com/alexsergivan/blog-examples/blob/master/alphabet/main.go">here</a></p>
<p>For more everyday Go mechanics, see <a href="/posts/mastering-for-loops-in-go/">mastering Golang for loops</a> and <a href="/posts/mastering-time-in-golang/">mastering time in Go</a> — both are full of small details that only bite in production.</p>]]></content:encoded>
      <category>Go Programming</category>
      <category>Internationalization</category>
    </item>
  </channel>
</rss>
