'Sort on map on value (attribute of Struct)

I have the below map:

detail := make(map[string]*Log)

type Log struct {
    Id      []string
    Name    []string
    Priority   int   // value could be 1, 2, 3
    Message    string
}

I want to sort the "detail" map on basis of the value which is a struct in my case. This should be sorted by attribute "Priority".

For example, Log (map of struct) may have values similar to below:

Z : &{[ba60] [XYZ] 3 "I am the boss"}
B : &{[ca50] [ABC] 2 "I am the Junior"}
U : &{[zc20] [PQR] 1 "I am the Newbie"}

I want them to print from increasing Priority order i.e. 1 to 3

U : &{[zc20] [PQR] 1 "I am the Newbie"}
B : &{[ca50] [ABC] 2 "I am the Junior"}
Z : &{[ba60] [XYZ] 3 "I am the boss"}

I tried to use the sort and implemented the Sort interface, but seems like still missing the clue somewhere. So, I implemented the below interface:

type byPriority []*Log

func (d byPriority) Len() int {
    return len(d)
}
func (d byPriority) Less(i, j int) bool {
    return d[i].Priority < d[j].Priority
}
func (d byPriority) Swap(i, j int) {
    d[i], d[j] = d[j], d[i]
}

But how should I apply sort.Sort() method on this map to get the sorted result. Do I need to add some more code?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source