Natural sorting in Go, see Wikipedia.
- Fast.
- Simple API.
- Dependency-free.
See these docs for more details.
Go version 1.18+
go get github.com/cristalhq/natsort
files := []string{"img12.png", "img10.png", "img2.png", "img1.png"}
fmt.Println("Lexicographically:")
sort.Strings(files)
for _, f := range files {
fmt.Println(f)
}
fmt.Println("\nNaturally:")
natsort.Sort(files)
for _, f := range files {
fmt.Println(f)
}
// Output:
// Lexicographically:
// img1.png
// img10.png
// img12.png
// img2.png
//
// Naturally:
// img1.png
// img2.png
// img10.png
// img12.png
See examples: example_test.go.