github.com/wtfutil/wtf@v0.43.0/modules/clocks/clock_collection.go (about)

     1  package clocks
     2  
     3  import (
     4  	"sort"
     5  	"time"
     6  )
     7  
     8  type ClockCollection struct {
     9  	Clocks []Clock
    10  }
    11  
    12  func (clocks *ClockCollection) Sorted(sortOrder string) []Clock {
    13  	if sortOrder == "natural" {
    14  		//no-op
    15  	} else if sortOrder == "chronological" {
    16  		clocks.SortedChronologically()
    17  	} else {
    18  		clocks.SortedAlphabetically()
    19  	}
    20  
    21  	return clocks.Clocks
    22  }
    23  
    24  func (clocks *ClockCollection) SortedAlphabetically() {
    25  	sort.Slice(clocks.Clocks, func(i, j int) bool {
    26  		clock := clocks.Clocks[i]
    27  		other := clocks.Clocks[j]
    28  
    29  		return clock.Label < other.Label
    30  	})
    31  }
    32  
    33  func (clocks *ClockCollection) SortedChronologically() {
    34  	now := time.Now()
    35  	sort.Slice(clocks.Clocks, func(i, j int) bool {
    36  		clock := clocks.Clocks[i]
    37  		other := clocks.Clocks[j]
    38  
    39  		return clock.ToLocal(now).String() < other.ToLocal(now).String()
    40  	})
    41  }