github.com/searKing/golang/go@v1.2.117/sort/sort.go (about) 1 // Copyright 2021 The searKing Author. 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. 4 5 package sort 6 7 import ( 8 "sort" 9 "time" 10 ) 11 12 // Convenience types for common cases 13 14 // TimeSlice attaches the methods of Interface to []time.Time, sorting in increasing order. 15 type TimeSlice []time.Time 16 17 func (x TimeSlice) Len() int { return len(x) } 18 func (x TimeSlice) Less(i, j int) bool { return x[i].Before(x[j]) } 19 func (x TimeSlice) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 20 21 // Sort is a convenience method: x.Sort() calls Sort(x). 22 func (x TimeSlice) Sort() { sort.Sort(x) } 23 24 // TimeDurationSlice attaches the methods of Interface to []time.Duration, sorting in increasing order. 25 type TimeDurationSlice []time.Duration 26 27 func (x TimeDurationSlice) Len() int { return len(x) } 28 func (x TimeDurationSlice) Less(i, j int) bool { return x[i] < x[j] } 29 func (x TimeDurationSlice) Swap(i, j int) { x[i], x[j] = x[j], x[i] } 30 31 // Sort is a convenience method: x.Sort() calls Sort(x). 32 func (x TimeDurationSlice) Sort() { sort.Sort(x) }