123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- package help
- type comparer func(interface{}, interface{}) int
- // Comparable is an interface that has to be implemented by a custom collection
- // elements in order to work with linq.
- //
- // Example:
- // func (f foo) CompareTo(c Comparable) int {
- // a, b := f.f1, c.(foo).f1
- //
- // if a < b {
- // return -1
- // } else if a > b {
- // return 1
- // }
- //
- // return 0
- // }
- type Comparable interface {
- CompareTo(Comparable) int
- }
- func getComparer(data interface{}) comparer {
- switch data.(type) {
- case int:
- return func(x, y interface{}) int {
- a, b := x.(int), y.(int)
- switch {
- case a > b:
- return 1
- case b > a:
- return -1
- default:
- return 0
- }
- }
- case int8:
- return func(x, y interface{}) int {
- a, b := x.(int8), y.(int8)
- switch {
- case a > b:
- return 1
- case b > a:
- return -1
- default:
- return 0
- }
- }
- case int16:
- return func(x, y interface{}) int {
- a, b := x.(int16), y.(int16)
- switch {
- case a > b:
- return 1
- case b > a:
- return -1
- default:
- return 0
- }
- }
- case int32:
- return func(x, y interface{}) int {
- a, b := x.(int32), y.(int32)
- switch {
- case a > b:
- return 1
- case b > a:
- return -1
- default:
- return 0
- }
- }
- case int64:
- return func(x, y interface{}) int {
- a, b := x.(int64), y.(int64)
- switch {
- case a > b:
- return 1
- case b > a:
- return -1
- default:
- return 0
- }
- }
- case uint:
- return func(x, y interface{}) int {
- a, b := x.(uint), y.(uint)
- switch {
- case a > b:
- return 1
- case b > a:
- return -1
- default:
- return 0
- }
- }
- case uint8:
- return func(x, y interface{}) int {
- a, b := x.(uint8), y.(uint8)
- switch {
- case a > b:
- return 1
- case b > a:
- return -1
- default:
- return 0
- }
- }
- case uint16:
- return func(x, y interface{}) int {
- a, b := x.(uint16), y.(uint16)
- switch {
- case a > b:
- return 1
- case b > a:
- return -1
- default:
- return 0
- }
- }
- case uint32:
- return func(x, y interface{}) int {
- a, b := x.(uint32), y.(uint32)
- switch {
- case a > b:
- return 1
- case b > a:
- return -1
- default:
- return 0
- }
- }
- case uint64:
- return func(x, y interface{}) int {
- a, b := x.(uint64), y.(uint64)
- switch {
- case a > b:
- return 1
- case b > a:
- return -1
- default:
- return 0
- }
- }
- case float32:
- return func(x, y interface{}) int {
- a, b := x.(float32), y.(float32)
- switch {
- case a > b:
- return 1
- case b > a:
- return -1
- default:
- return 0
- }
- }
- case float64:
- return func(x, y interface{}) int {
- a, b := x.(float64), y.(float64)
- switch {
- case a > b:
- return 1
- case b > a:
- return -1
- default:
- return 0
- }
- }
- case string:
- return func(x, y interface{}) int {
- a, b := x.(string), y.(string)
- switch {
- case a > b:
- return 1
- case b > a:
- return -1
- default:
- return 0
- }
- }
- case bool:
- return func(x, y interface{}) int {
- a, b := x.(bool), y.(bool)
- switch {
- case a == b:
- return 0
- case a:
- return 1
- default:
- return -1
- }
- }
- default:
- return func(x, y interface{}) int {
- a, b := x.(Comparable), y.(Comparable)
- return a.CompareTo(b)
- }
- }
- }
|