title: Sort structure on a string key with accented characters
body: <p>I recently stumbled upon a problem trying to sort a structure on a key containing french strings. The classical <code>sort.SliceStable</code> doesn't take the accented characters into account correctly according the french rules for alphabetical sort.</p>
  <p>Thankfully, the <a href="https://pkg.go.dev/golang.org/x/text/collate#Collator.Key" rel="nofollow noreferrer">collate</a> package does that correctly, but I couldn't find a way to directly use that package on structures without using it combined with the <code>sort.SliceStable</code> function.</p>
  <p>This what I came up with that seems to work correctly:</p>
  <pre><code>import (
      &quot;fmt&quot;
      &quot;sort&quot;
  
      &quot;golang.org/x/text/collate&quot;
      &quot;golang.org/x/text/language&quot;
  )
  
  type SortFr struct {
      Order int
      Fr    string
  }
  
  func main() {
  
      // the int SortFr.Order reflects the correct french sort order
      s := []SortFr{
          SortFr{2, &quot;côte&quot;},
          SortFr{0, &quot;cote&quot;},
          SortFr{5, &quot;coter&quot;},
          SortFr{1, &quot;coté&quot;},
          SortFr{4, &quot;cotée&quot;},
          SortFr{3, &quot;côté&quot;},
      }
  
      // incorrect sort order 
      sort.SliceStable(s, func(i, j int) bool {
          return s[i].Fr &lt; s[j].Fr
      })
      fmt.Println(s)
  
      // correct sort order
      cl := collate.New(language.French)
      sort.SliceStable(s, func(i, j int) bool {
          return cl.CompareString(s[i].Fr, s[j].Fr) == -1
      })
      fmt.Println(s)
  
  }
  </code></pre>
  <p>This code returns:</p>
  <pre><code>[{0 cote} {5 coter} {1 coté} {4 cotée} {2 côte} {3 côté}]
  [{0 cote} {1 coté} {2 côte} {3 côté} {4 cotée} {5 coter}]
  </code></pre>
  <p>Is this the most efficient way to do this on large structures?</p>