Skip to content

Commit

Permalink
Update file ext display with fallback sort condition for stability
Browse files Browse the repository at this point in the history
  • Loading branch information
naftalibeder committed Aug 17, 2024
1 parent 68bbc4d commit bc59247
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
5 changes: 0 additions & 5 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 24 additions & 14 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, ", ")
}

0 comments on commit bc59247

Please sign in to comment.