github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+incompatible/pkg/releaseutil/sorter.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 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 "k8s.io/helm/pkg/releaseutil" 18 19 import ( 20 "sort" 21 22 rspb "k8s.io/helm/pkg/proto/hapi/release" 23 ) 24 25 type sorter struct { 26 list []*rspb.Release 27 less func(int, int) bool 28 } 29 30 func (s *sorter) Len() int { return len(s.list) } 31 func (s *sorter) Less(i, j int) bool { return s.less(i, j) } 32 func (s *sorter) Swap(i, j int) { s.list[i], s.list[j] = s.list[j], s.list[i] } 33 34 // Reverse reverses the list of releases sorted by the sort func. 35 func Reverse(list []*rspb.Release, sortFn func([]*rspb.Release)) { 36 sortFn(list) 37 for i, j := 0, len(list)-1; i < j; i, j = i+1, j-1 { 38 list[i], list[j] = list[j], list[i] 39 } 40 } 41 42 // SortByName returns the list of releases sorted 43 // in lexicographical order. 44 func SortByName(list []*rspb.Release) { 45 s := &sorter{list: list} 46 s.less = func(i, j int) bool { 47 ni := s.list[i].Name 48 nj := s.list[j].Name 49 return ni < nj 50 } 51 sort.Sort(s) 52 } 53 54 // SortByDate returns the list of releases sorted by a 55 // release's last deployed time (in seconds). 56 func SortByDate(list []*rspb.Release) { 57 s := &sorter{list: list} 58 59 s.less = func(i, j int) bool { 60 ti := s.list[i].Info.LastDeployed.Seconds 61 tj := s.list[j].Info.LastDeployed.Seconds 62 return ti < tj 63 } 64 sort.Sort(s) 65 } 66 67 // SortByRevision returns the list of releases sorted by a 68 // release's revision number (release.Version). 69 func SortByRevision(list []*rspb.Release) { 70 s := &sorter{list: list} 71 s.less = func(i, j int) bool { 72 vi := s.list[i].Version 73 vj := s.list[j].Version 74 return vi < vj 75 } 76 sort.Sort(s) 77 }