The sort.Interface defines three methods which must be implemented:
// Len is the number of elements in the collection.
Len() int
// Less reports whether the element with
// index i should sort before the element with index j.
Less(i, j int) bool
// Swap swaps the elements with indexes i and j.
Swap(i, j int)
In this context this would look something like:
type ExternalType struct {
quantity int
}
type ExternalArray []*ExternalType
func (ea ExternalArray) Len() int {
return len(ea)
}
func (ea ExternalArray) Less(i, j int) bool {
return ea[i].quantity < ea[j].quantity
}
func (ea ExternalArray) Swap(i, j int) {
ea[i], ea[j] = ea[j], ea[i]
}
In order to do the sort you can then use sort.Sort, for example:
arr := ExternalArray{
&ExternalType{quantity: 33},
&ExternalType{quantity: 44},
&ExternalType{quantity: 22},
&ExternalType{quantity: 11},
}
sort.Sort(arr)
// `arr` is now sorted :-)
Here is a working example in the playground.