github.com/seeker-insurance/kit@v0.0.13/counter/pair.go (about)

     1  package counter
     2  
     3  //Pair represents a Key, Count pair
     4  type Pair struct {
     5  	Key   string
     6  	Count int
     7  }
     8  
     9  //Pairs is a slice of Pairs. It is sortable lexigraphically by count, then key.
    10  type Pairs []Pair
    11  
    12  func (p Pair) KeyVal() (string, int) { return p.Key, p.Count }
    13  
    14  func (pairs Pairs) Less(i, j int) bool {
    15  	a, b := pairs[i].Count, pairs[j].Count
    16  	return a < b || (a <= b && pairs[i].Key < pairs[j].Key)
    17  }
    18  func (pairs Pairs) Len() int      { return len(pairs) }
    19  func (pairs Pairs) Swap(i, j int) { pairs[i], pairs[j] = pairs[j], pairs[i] }
    20  
    21  func (pairs Pairs) Keys() []string {
    22  	keys := make([]string, len(pairs))
    23  	for i, p := range pairs {
    24  		keys[i] = p.Key
    25  	}
    26  	return keys
    27  }
    28  func (pairs Pairs) Counts() []int {
    29  	counts := make([]int, len(pairs))
    30  	for i, c := range pairs {
    31  		counts[i] = c.Count
    32  	}
    33  	return counts
    34  }