File: example_sort_test.go

package info (click to toggle)
golang-golang-x-text 0.22.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie, trixie-backports
  • size: 41,612 kB
  • sloc: xml: 1,022; makefile: 6
file content (56 lines) | stat: -rw-r--r-- 986 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
}