Site Reliability Engineering

Sort an array of structs in Golang

Example how to sort an array of struct's by one of the struct fields.

We sort ServicePayList which is an array of ServicePay struct's by the integer field Payment.


type ServicePay struct {
	Name string
	Payment int
}
var ServicePayList []cost_types.ServicePay
func comparePrice(i, j int) bool {
   return ServicePayList[i].Payment > ServicePayList[j].Payment
}
sort.Slice(ServicePayList, comparePrice)