feat: support asc/dec sorting

Closes coop-cloud/tagcmp#1.
This commit is contained in:
2021-09-06 12:20:06 +02:00
parent 4ef558bb7a
commit 2a8edd82d7
4 changed files with 100 additions and 24 deletions

View File

@ -18,15 +18,24 @@ type Tag struct {
UsesV bool // whether or not the tag uses the "v" prefix
}
// ByTag sorts tags in asc/desc order where the last element is the latest tag.
type ByTag []Tag
// ByTagAsc sorts tags in ascending order where the last element is the latest tag.
type ByTagAsc []Tag
func (t ByTag) Len() int { return len(t) }
func (t ByTag) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t ByTag) Less(i, j int) bool {
func (t ByTagAsc) Len() int { return len(t) }
func (t ByTagAsc) Swap(i, j int) { t[i], t[j] = t[j], t[i] }
func (t ByTagAsc) Less(i, j int) bool {
return t[i].IsLessThan(t[j])
}
// ByTagDesc sorts tags in descending order where the first element is the latest tag.
type ByTagDesc []Tag
func (t ByTagDesc) Len() int { return len(t) }
func (t ByTagDesc) Swap(i, j int) { t[j], t[i] = t[i], t[j] }
func (t ByTagDesc) Less(i, j int) bool {
return t[j].IsLessThan(t[i])
}
// IsGreaterThan tests if a tag is greater than another. There are some
// tag-isms to take into account here, shorter is bigger (i.e. 2.1 > 2.1.1 ==
// true, 2 > 2.1 == true).