text/collate: add testable examples

Change-Id: Id3915137c4d365ec82ce74d8212e7b6cfb6fb200
Reviewed-on: https://go-review.googlesource.com/c/text/+/360494
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Trust: Marcel van Lohuizen <mpvl@golang.org>
diff --git a/collate/example_sort_test.go b/collate/example_sort_test.go
new file mode 100644
index 0000000..e86c02a
--- /dev/null
+++ b/collate/example_sort_test.go
@@ -0,0 +1,56 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package collate_test
+
+import (
+	"fmt"
+
+	"golang.org/x/text/collate"
+	"golang.org/x/text/language"
+)
+
+type book struct {
+	title string
+}
+
+type bookcase struct {
+	books []book
+}
+
+func (bc bookcase) Len() int {
+	return len(bc.books)
+}
+
+func (bc bookcase) Swap(i, j int) {
+	temp := bc.books[i]
+	bc.books[i] = bc.books[j]
+	bc.books[j] = temp
+}
+
+func (bc bookcase) Bytes(i int) []byte {
+	// returns the bytes of text at index i
+	return []byte(bc.books[i].title)
+}
+
+func ExampleCollator_Sort() {
+	bc := bookcase{
+		books: []book{
+			{title: "If Cats Disappeared from the World"},
+			{title: "The Guest Cat"},
+			{title: "Catwings"},
+		},
+	}
+
+	cc := collate.New(language.English)
+	cc.Sort(bc)
+
+	for _, b := range bc.books {
+		fmt.Println(b.title)
+	}
+	// Output:
+	// Catwings
+	// If Cats Disappeared from the World
+	// The Guest Cat
+}
diff --git a/collate/examples_test.go b/collate/examples_test.go
new file mode 100644
index 0000000..0a42a6d
--- /dev/null
+++ b/collate/examples_test.go
@@ -0,0 +1,79 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package collate_test
+
+import (
+	"fmt"
+
+	"golang.org/x/text/collate"
+	"golang.org/x/text/language"
+)
+
+func ExampleNew() {
+	letters := []string{"ä", "å", "ö", "o", "a"}
+
+	ec := collate.New(language.English)
+	ec.SortStrings(letters)
+	fmt.Printf("English Sorting: %v\n", letters)
+
+	sc := collate.New(language.Swedish)
+	sc.SortStrings(letters)
+	fmt.Printf("Swedish Sorting: %v\n", letters)
+
+	numbers := []string{"0", "11", "01", "2", "3", "23"}
+
+	ec.SortStrings(numbers)
+	fmt.Printf("Alphabetic Sorting: %v\n", numbers)
+
+	nc := collate.New(language.English, collate.Numeric)
+	nc.SortStrings(numbers)
+	fmt.Printf("Numeric Sorting: %v\n", numbers)
+	// Output:
+	// English Sorting: [a å ä o ö]
+	// Swedish Sorting: [a o å ä ö]
+	// Alphabetic Sorting: [0 01 11 2 23 3]
+	// Numeric Sorting: [0 01 2 3 11 23]
+}
+
+func ExampleCollator_SortStrings() {
+	c := collate.New(language.English)
+	words := []string{"meow", "woof", "bark", "moo"}
+	c.SortStrings(words)
+	fmt.Println(words)
+	// Output:
+	// [bark meow moo woof]
+}
+
+func ExampleCollator_CompareString() {
+	c := collate.New(language.English)
+	r := c.CompareString("meow", "woof")
+	fmt.Println(r)
+
+	r = c.CompareString("woof", "meow")
+	fmt.Println(r)
+
+	r = c.CompareString("meow", "meow")
+	fmt.Println(r)
+	// Output:
+	// -1
+	// 1
+	// 0
+}
+
+func ExampleCollator_Compare() {
+	c := collate.New(language.English)
+	r := c.Compare([]byte("meow"), []byte("woof"))
+	fmt.Println(r)
+
+	r = c.Compare([]byte("woof"), []byte("meow"))
+	fmt.Println(r)
+
+	r = c.Compare([]byte("meow"), []byte("meow"))
+	fmt.Println(r)
+	// Output:
+	// -1
+	// 1
+	// 0
+}