-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
27 lines (23 loc) · 650 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package songmem
import (
"sort"
)
type songRating struct {
Song string
Rating float64
}
// songRatingsToSongs returns a slice of songs, ordered by their rating.
func songRatingsToSongs(ratingsMap map[string]float64) []string {
songRatings := make([]songRating, 0, len(ratingsMap))
for song, rating := range ratingsMap {
songRatings = append(songRatings, songRating{song, rating})
}
sort.Slice(songRatings, func(i, j int) bool {
return songRatings[i].Rating > songRatings[j].Rating
})
songs := make([]string, 0, len(songRatings))
for _, songRating := range songRatings {
songs = append(songs, songRating.Song)
}
return songs
}