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.
17 type swapper interface {
27 func (s *sorter) init(n int) {
33 s.keys = make([][]byte, n)
38 func (s *sorter) sort(src swapper) {
43 func (s sorter) Len() int {
47 func (s sorter) Less(i, j int) bool {
48 return bytes.Compare(s.keys[i], s.keys[j]) == -1
51 func (s sorter) Swap(i, j int) {
52 s.keys[i], s.keys[j] = s.keys[j], s.keys[i]
56 // A Lister can be sorted by Collator's Sort method.
57 type Lister interface {
60 // Bytes returns the bytes of the text at index i.
64 // Sort uses sort.Sort to sort the strings represented by x using the rules of c.
65 func (c *Collator) Sort(x Lister) {
68 for i := 0; i < n; i++ {
69 c.sorter.keys[i] = c.Key(c.sorter.buf, x.Bytes(i))
74 // SortStrings uses sort.Sort to sort the strings in x using the rules of c.
75 func (c *Collator) SortStrings(x []string) {
78 c.sorter.keys[i] = c.KeyFromString(c.sorter.buf, s)
80 c.sorter.sort(sort.StringSlice(x))