https://github.com/icza/abcsort
Go string sorting library that uses a custom, user-defined alphabet
https://github.com/icza/abcsort
alphabet sort
Last synced: 2 months ago
JSON representation
Go string sorting library that uses a custom, user-defined alphabet
- Host: GitHub
- URL: https://github.com/icza/abcsort
- Owner: icza
- License: apache-2.0
- Created: 2018-12-25T17:09:32.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-30T13:37:25.000Z (over 2 years ago)
- Last Synced: 2025-04-10T12:15:43.021Z (6 months ago)
- Topics: alphabet, sort
- Language: Go
- Homepage:
- Size: 24.4 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# abcsort

[](https://pkg.go.dev/github.com/icza/abcsort)
[](https://goreportcard.com/report/github.com/icza/abcsort)
[](https://codecov.io/gh/icza/abcsort)Go string sorting library that uses a custom, user-defined alphabet.
Implementation does not convert the input strings into byte or rune slices, so
performance is rather good.Custom sorting can be easiest achieved by using the `Sorter` helper type, for example:
sorter := abcsort.New("bac")
ss := []string{"abc", "bac", "cba", "CCC"}
sorter.Strings(ss)
fmt.Println(ss)ss = []string{"abc", "bac", "cba", "CCC"}
sorter.StringsFold(ss)
fmt.Println(ss)type Person struct {
Name string
Age int
}ps := []Person{{Name: "alice", Age: 21}, {Name: "bob", Age: 12}}
sorter.Slice(ps, func(i int) string { return ps[i].Name })
fmt.Println(ps)ps = []Person{{Name: "Alice", Age: 21}, {Name: "Bob", Age: 12}}
sorter.SliceFold(ps, func(i int) string { return ps[i].Name })
fmt.Println(ps)// Output:
// [CCC bac abc cba]
// [bac abc cba CCC]
// [{bob 12} {alice 21}]
// [{Bob 12} {Alice 21}]The essence of sorting, the `less()` function required by the standard lib's `sort`
package is also exposed, and may be used "manually" like this:weights := Weights("bac")
ss := []string{"abc", "bac", "cba", "CCC"}
sort.Slice(ss, func(i int, j int) bool {
return Less(ss[i], ss[j], weights)
})
fmt.Println(ss)// Output:
// [CCC bac abc cba]Or via the `StringSlice` type:
weights := Weights("bac")
ss := []string{"abc", "bac", "cba", "CCC"}
strslice := &StringSlice{
Weights: weights,
Slice: ss,
}
sort.Sort(strslice)
fmt.Println(ss)// Output:
// [CCC bac abc cba]