在sort.Interface定义了三个方法必须实现:// 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)在这种情况下,这看起来像:type ExternalType struct { quantity int}type ExternalArray []*ExternalTypefunc (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]}为了进行排序,您可以使用sort.Sort,例如:arr := ExternalArray{ &ExternalType{quantity: 33}, &ExternalType{quantity: 44}, &ExternalType{quantity: 22}, &ExternalType{quantity: 11},}sort.Sort(arr)// `arr` is now sorted :-)这是操场上的一个工作示例。