Skip to content

Instantly share code, notes, and snippets.

@hipertracker
Last active February 27, 2020 11:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hipertracker/fd0ea69cc68a70270864dac1079bbaa2 to your computer and use it in GitHub Desktop.
Save hipertracker/fd0ea69cc68a70270864dac1079bbaa2 to your computer and use it in GitHub Desktop.
Golang: sorting a slice of string with Polish sorting rules
package main
import (
"fmt"
"sort"
"github.com/tidwall/collate"
)
type text []string
func main() {
arr := text{"b", "ą", "ć", "c", "a", "ż", "ś"}
sort.Sort(arr)
fmt.Printf("%+v", arr)
}
func (t text) Less(i, j int) bool {
less := collate.IndexString("POLISH_CI")
return less(t[i], t[j])
}
func (t text) Len() int {
return len(t)
}
func (t text) Swap(i, j int) {
t[i], t[j] = t[j], t[i]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment