...

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

Documentation: golang.org/x/text/collate

     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  
     5  package collate_test
     6  
     7  import (
     8  	"fmt"
     9  	"testing"
    10  
    11  	"golang.org/x/text/collate"
    12  	"golang.org/x/text/language"
    13  )
    14  
    15  func ExampleCollator_Strings() {
    16  	c := collate.New(language.Und)
    17  	strings := []string{
    18  		"ad",
    19  		"ab",
    20  		"äb",
    21  		"ac",
    22  	}
    23  	c.SortStrings(strings)
    24  	fmt.Println(strings)
    25  	// Output: [ab äb ac ad]
    26  }
    27  
    28  type sorter []string
    29  
    30  func (s sorter) Len() int {
    31  	return len(s)
    32  }
    33  
    34  func (s sorter) Swap(i, j int) {
    35  	s[j], s[i] = s[i], s[j]
    36  }
    37  
    38  func (s sorter) Bytes(i int) []byte {
    39  	return []byte(s[i])
    40  }
    41  
    42  func TestSort(t *testing.T) {
    43  	c := collate.New(language.English)
    44  	strings := []string{
    45  		"bcd",
    46  		"abc",
    47  		"ddd",
    48  	}
    49  	c.Sort(sorter(strings))
    50  	res := fmt.Sprint(strings)
    51  	want := "[abc bcd ddd]"
    52  	if res != want {
    53  		t.Errorf("found %s; want %s", res, want)
    54  	}
    55  }
    56  

View as plain text