sort_test.go 924 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2013 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package collate_test
  5. import (
  6. "fmt"
  7. "testing"
  8. "golang.org/x/text/collate"
  9. "golang.org/x/text/language"
  10. )
  11. func ExampleCollator_Strings() {
  12. c := collate.New(language.Und)
  13. strings := []string{
  14. "ad",
  15. "ab",
  16. "äb",
  17. "ac",
  18. }
  19. c.SortStrings(strings)
  20. fmt.Println(strings)
  21. // Output: [ab äb ac ad]
  22. }
  23. type sorter []string
  24. func (s sorter) Len() int {
  25. return len(s)
  26. }
  27. func (s sorter) Swap(i, j int) {
  28. s[j], s[i] = s[i], s[j]
  29. }
  30. func (s sorter) Bytes(i int) []byte {
  31. return []byte(s[i])
  32. }
  33. func TestSort(t *testing.T) {
  34. c := collate.New(language.English)
  35. strings := []string{
  36. "bcd",
  37. "abc",
  38. "ddd",
  39. }
  40. c.Sort(sorter(strings))
  41. res := fmt.Sprint(strings)
  42. want := "[abc bcd ddd]"
  43. if res != want {
  44. t.Errorf("found %s; want %s", res, want)
  45. }
  46. }