...

Source file src/golang.org/x/text/collate/example_sort_test.go

Documentation: golang.org/x/text/collate

     1  // Copyright 2021 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  
     5  package collate_test
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"golang.org/x/text/collate"
    11  	"golang.org/x/text/language"
    12  )
    13  
    14  type book struct {
    15  	title string
    16  }
    17  
    18  type bookcase struct {
    19  	books []book
    20  }
    21  
    22  func (bc bookcase) Len() int {
    23  	return len(bc.books)
    24  }
    25  
    26  func (bc bookcase) Swap(i, j int) {
    27  	temp := bc.books[i]
    28  	bc.books[i] = bc.books[j]
    29  	bc.books[j] = temp
    30  }
    31  
    32  func (bc bookcase) Bytes(i int) []byte {
    33  	// returns the bytes of text at index i
    34  	return []byte(bc.books[i].title)
    35  }
    36  
    37  func ExampleCollator_Sort() {
    38  	bc := bookcase{
    39  		books: []book{
    40  			{title: "If Cats Disappeared from the World"},
    41  			{title: "The Guest Cat"},
    42  			{title: "Catwings"},
    43  		},
    44  	}
    45  
    46  	cc := collate.New(language.English)
    47  	cc.Sort(bc)
    48  
    49  	for _, b := range bc.books {
    50  		fmt.Println(b.title)
    51  	}
    52  	// Output:
    53  	// Catwings
    54  	// If Cats Disappeared from the World
    55  	// The Guest Cat
    56  }
    57  

View as plain text