...

Source file src/golang.org/x/text/collate/examples_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  func ExampleNew() {
    15  	letters := []string{"ä", "å", "ö", "o", "a"}
    16  
    17  	ec := collate.New(language.English)
    18  	ec.SortStrings(letters)
    19  	fmt.Printf("English Sorting: %v\n", letters)
    20  
    21  	sc := collate.New(language.Swedish)
    22  	sc.SortStrings(letters)
    23  	fmt.Printf("Swedish Sorting: %v\n", letters)
    24  
    25  	numbers := []string{"0", "11", "01", "2", "3", "23"}
    26  
    27  	ec.SortStrings(numbers)
    28  	fmt.Printf("Alphabetic Sorting: %v\n", numbers)
    29  
    30  	nc := collate.New(language.English, collate.Numeric)
    31  	nc.SortStrings(numbers)
    32  	fmt.Printf("Numeric Sorting: %v\n", numbers)
    33  	// Output:
    34  	// English Sorting: [a å ä o ö]
    35  	// Swedish Sorting: [a o å ä ö]
    36  	// Alphabetic Sorting: [0 01 11 2 23 3]
    37  	// Numeric Sorting: [0 01 2 3 11 23]
    38  }
    39  
    40  func ExampleCollator_SortStrings() {
    41  	c := collate.New(language.English)
    42  	words := []string{"meow", "woof", "bark", "moo"}
    43  	c.SortStrings(words)
    44  	fmt.Println(words)
    45  	// Output:
    46  	// [bark meow moo woof]
    47  }
    48  
    49  func ExampleCollator_CompareString() {
    50  	c := collate.New(language.English)
    51  	r := c.CompareString("meow", "woof")
    52  	fmt.Println(r)
    53  
    54  	r = c.CompareString("woof", "meow")
    55  	fmt.Println(r)
    56  
    57  	r = c.CompareString("meow", "meow")
    58  	fmt.Println(r)
    59  	// Output:
    60  	// -1
    61  	// 1
    62  	// 0
    63  }
    64  
    65  func ExampleCollator_Compare() {
    66  	c := collate.New(language.English)
    67  	r := c.Compare([]byte("meow"), []byte("woof"))
    68  	fmt.Println(r)
    69  
    70  	r = c.Compare([]byte("woof"), []byte("meow"))
    71  	fmt.Println(r)
    72  
    73  	r = c.Compare([]byte("meow"), []byte("meow"))
    74  	fmt.Println(r)
    75  	// Output:
    76  	// -1
    77  	// 1
    78  	// 0
    79  }
    80  

View as plain text