编辑:
考虑此用户类型:
type userType struct { frequency int value rune}并假设您想将此类型添加到“链接列表”:
并且应首先按频率对其进行排序,然后,如果频率相同,请查看char值。因此该
Compare函数将是:
func (a userType) Compare(b userType) int { if a.frequency > b.frequency { return 1 } if a.frequency < b.frequency { return -1 } if a.value > b.value { return 1 } if a.value < b.value { return -1 } return 0}满足以下接口:
type Comparer interface { Compare(b userType) int}现在将这些
{1,'d'} {2,'b'} {3,'c'} {4,'a'} {4,'b'} {4,'c'}类型添加到linkeList:示例代码:
package mainimport ( "container/list" "fmt")type Comparer interface { Compare(b userType) int}type userType struct { frequency int value rune}// it should sort by frequency first, then if the frequencies are the same, look at the char value.func (a userType) Compare(b userType) int { if a.frequency > b.frequency { return 1 } if a.frequency < b.frequency { return -1 } if a.value > b.value { return 1 } if a.value < b.value { return -1 } return 0}func Insert(val userType, l *list.List) { e := l.Front() if e == nil { l.PushFront(val) return } for ; e != nil; e = e.Next() { var ut userType = e.Value.(userType) if val.Compare(ut) < 0 { l.InsertBefore(val, e) return } } l.PushBack(val)}func main() { l := list.New() Insert(userType{4, 'c'}, l) Insert(userType{4, 'a'}, l) Insert(userType{4, 'b'}, l) Insert(userType{2, 'b'}, l) Insert(userType{3, 'c'}, l) Insert(userType{1, 'd'}, l) for e := l.Front(); e != nil; e = e.Next() { ut := e.Value.(userType) fmt.Printf("{%d,%q} ", ut.frequency, ut.value) } fmt.Println() var t interface{} = userType{4, 'c'} i, ok := t.(Comparer) fmt.Println(i, ok)}并输出:
{1,'d'} {2,'b'} {3,'c'} {4,'a'} {4,'b'} {4,'c'} {4 99} true因此,如果您准备使用已知类型(例如
int),请参见以下示例:
package mainimport ( "container/list" "fmt")func Insert(val int, l *list.List) { e := l.Front() if e == nil { l.PushFront(val) return } for ; e != nil; e = e.Next() { v := e.Value.(int) if val < v { l.InsertBefore(val, e) return } } l.PushBack(val)}func main() { l := list.New() Insert(4, l) Insert(2, l) Insert(3, l) Insert(1, l) for e := l.Front(); e != nil; e = e.Next() { fmt.Print(e.Value, " ") // 1 2 3 4 } fmt.Println()}旧:
Go中没有这样的界面。您可以编写此
Less函数来比较您的类型:
func Less(a, b interface{}) bool { switch a.(type) { case int: if ai, ok := a.(int); ok { if bi, ok := b.(int); ok { return ai < bi } } case string: if ai, ok := a.(string); ok { if bi, ok := b.(string); ok { return ai < bi } } // ... default: panic("Unknown") } return false}测试示例代码:
package mainimport ( "container/list" "fmt")func Less(a, b interface{}) bool { switch a.(type) { case int: if ai, ok := a.(int); ok { if bi, ok := b.(int); ok { return ai < bi } } case string: if ai, ok := a.(string); ok { if bi, ok := b.(string); ok { return ai < bi } } default: panic("Unknown") } return false}func Insert(val interface{}, l *list.List) *list.Element { e := l.Front() if e == nil { return l.PushFront(val) } for ; e != nil; e = e.Next() { if Less(val, e.Value) { return l.InsertBefore(val, e) } } return l.PushBack(val)}func main() { l := list.New() Insert(4, l) Insert(2, l) Insert(3, l) Insert(1, l) for e := l.Front(); e != nil; e = e.Next() { fmt.Print(e.Value, " ") } fmt.Println() Insert("C", l) Insert("A", l) Insert("AB", l) Insert("C", l) Insert("C2", l) Insert("C1", l) for e := l.Front(); e != nil; e = e.Next() { fmt.Print(e.Value, " ") } fmt.Println()}输出:
1 2 3 4 1 2 3 4 A AB C C C1 C2



