github.com/go-graphite/carbonapi@v0.17.0/expr/helper/sort.go (about)

     1  package helper
     2  
     3  import (
     4  	"github.com/go-graphite/carbonapi/expr/types"
     5  	"github.com/maruel/natural"
     6  )
     7  
     8  // ByVals sorts by values
     9  // Total (sortByTotal), max (sortByMaxima), min (sortByMinima) sorting
    10  // For 'min', we actually store 1/v so the sorting logic is the same
    11  type ByVals struct {
    12  	Vals   []float64
    13  	Series []*types.MetricData
    14  }
    15  
    16  // Len returns length, required to be sortable
    17  func (s ByVals) Len() int { return len(s.Series) }
    18  
    19  // Swap swaps to elements by IDs, required to be sortable
    20  func (s ByVals) Swap(i, j int) {
    21  	s.Series[i], s.Series[j] = s.Series[j], s.Series[i]
    22  	s.Vals[i], s.Vals[j] = s.Vals[j], s.Vals[i]
    23  }
    24  
    25  // Less compares two elements with specified IDs, required to be sortable
    26  func (s ByVals) Less(i, j int) bool {
    27  	return s.Vals[i] < s.Vals[j]
    28  }
    29  
    30  // ByName sorts metrics by name
    31  type ByName []*types.MetricData
    32  
    33  // Len returns length, required to be sortable
    34  func (s ByName) Len() int { return len(s) }
    35  
    36  // Swap swaps to elements by IDs, required to be sortable
    37  func (s ByName) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
    38  
    39  // Less compares two elements with specified IDs, required to be sortable
    40  func (s ByName) Less(i, j int) bool { return s[i].Name < s[j].Name }
    41  
    42  // ByNameNatural sorts metric naturally by name
    43  type ByNameNatural []*types.MetricData
    44  
    45  // Len returns length, required to be sortable
    46  func (s ByNameNatural) Len() int { return len(s) }
    47  
    48  // Swap swaps to elements by IDs, required to be sortable
    49  func (s ByNameNatural) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
    50  
    51  // Less compares two elements with specified IDs, required to be sortable
    52  func (s ByNameNatural) Less(i, j int) bool { return natural.Less(s[i].Name, s[j].Name) }