github.com/qsis/helm@v3.0.0-beta.3+incompatible/pkg/releaseutil/sorter.go (about)

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package releaseutil // import "helm.sh/helm/pkg/releaseutil"
    18  
    19  import (
    20  	"sort"
    21  
    22  	rspb "helm.sh/helm/pkg/release"
    23  )
    24  
    25  type list []*rspb.Release
    26  
    27  func (s list) Len() int      { return len(s) }
    28  func (s list) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
    29  
    30  // ByName sorts releases by name
    31  type ByName struct{ list }
    32  
    33  // Less compares to releases
    34  func (s ByName) Less(i, j int) bool { return s.list[i].Name < s.list[j].Name }
    35  
    36  // ByDate sorts releases by date
    37  type ByDate struct{ list }
    38  
    39  // Less compares to releases
    40  func (s ByDate) Less(i, j int) bool {
    41  	ti := s.list[i].Info.LastDeployed.Unix()
    42  	tj := s.list[j].Info.LastDeployed.Unix()
    43  	return ti < tj
    44  }
    45  
    46  // ByRevision sorts releases by revision number
    47  type ByRevision struct{ list }
    48  
    49  // Less compares to releases
    50  func (s ByRevision) Less(i, j int) bool {
    51  	return s.list[i].Version < s.list[j].Version
    52  }
    53  
    54  // Reverse reverses the list of releases sorted by the sort func.
    55  func Reverse(list []*rspb.Release, sortFn func([]*rspb.Release)) {
    56  	sortFn(list)
    57  	for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 {
    58  		list[i], list[j] = list[j], list[i]
    59  	}
    60  }
    61  
    62  // SortByName returns the list of releases sorted
    63  // in lexicographical order.
    64  func SortByName(list []*rspb.Release) {
    65  	sort.Sort(ByName{list})
    66  }
    67  
    68  // SortByDate returns the list of releases sorted by a
    69  // release's last deployed time (in seconds).
    70  func SortByDate(list []*rspb.Release) {
    71  	sort.Sort(ByDate{list})
    72  }
    73  
    74  // SortByRevision returns the list of releases sorted by a
    75  // release's revision number (release.Version).
    76  func SortByRevision(list []*rspb.Release) {
    77  	sort.Sort(ByRevision{list})
    78  }