diff --git a/types/types.go b/types/types.go index 1920779..37a3e9b 100644 --- a/types/types.go +++ b/types/types.go @@ -69,11 +69,6 @@ type VidCodecCtMap map[string]int // Map of audio codec to occurrence count. type AudCodecCtMap map[string]int -type SortableItem struct { - Value string - Count int -} - type ExtCount struct { Ext string Count int diff --git a/utils/utils.go b/utils/utils.go index 36f505d..34be1f1 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -104,26 +104,36 @@ func GetDateFromStr(s string) (date time.Time, err error) { } // Returns a comma-separated list from the provided map, ordered by each -// entry's int value. +// entry's value. func SortedListFromCt(m map[string]int) string { - items := []types.SortableItem{} + type Item struct { + Key string + CtVal int + } + + items := []Item{} - for val, ct := range m { - items = append(items, types.SortableItem{ - Value: val, - Count: ct, + for k, ctVal := range m { + items = append(items, Item{ + Key: k, + CtVal: ctVal, }) } + sort.SliceStable(items, func(i, j int) bool { - return items[i].Count > items[j].Count + prev := items[i] + next := items[j] + if prev.CtVal == next.CtVal { + return prev.Key > next.Key + } else { + return prev.CtVal > next.CtVal + } }) - disp := "" - for i, item := range items { - if i > 0 { - disp += ", " - } - disp += fmt.Sprintf("%s %d", item.Value, item.Count) + pairs := []string{} + for _, item := range items { + pairs = append(pairs, fmt.Sprintf("%s %d", item.Key, item.CtVal)) } - return disp + + return strings.Join(pairs, ", ") }