Last active
February 27, 2020 11:11
-
-
Save hipertracker/fd0ea69cc68a70270864dac1079bbaa2 to your computer and use it in GitHub Desktop.
Golang: sorting a slice of string with Polish sorting rules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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