This question already has an answer here:
I have a slice in golang which looks something like this.
list := []TripInfo{
{
TripID: "uuid2",
infov:true
},
{
TripID: "uuid1",
infov:false
},
}
How can I sort it based on TripID
so that it looks something like this?
list := []TripInfo{
{
TripID: "uuid1",
infov:false
},
{
TripID: "uuid2",
infov:true
},
}
TripInfo is a struct
type TripInfo struct {
TripUUID string
infov bool
}
</div>
The sort pkg is your friend:
import "sort"
// sort slice in place
sort.Slice(list, func(i, j int) bool {
return list[i].TripID < list[j].TripID
})