github.com/hamba/slices@v0.2.1-0.20220316050741-75c057d92699/greater.go (about)

     1  package slices
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  type greaterFn func(unsafe.Pointer) func(i, j int) bool
     8  
     9  // GreaterOf returns a greater function for slice sorting.
    10  func GreaterOf(slice interface{}) func(i, j int) bool {
    11  	fn := greaterOf(slice)
    12  	if fn == nil {
    13  		panic("slice is not supported slice type")
    14  	}
    15  
    16  	ptr := noescape(ptrOf(slice))
    17  	return fn(ptr)
    18  }
    19  
    20  func greaterOf(slice interface{}) greaterFn {
    21  	switch slice.(type) {
    22  	case []string:
    23  		return stringGreater
    24  	case []int:
    25  		return intGreater
    26  	case []int8:
    27  		return int8Greater
    28  	case []int16:
    29  		return int16Greater
    30  	case []int32:
    31  		return int32Greater
    32  	case []int64:
    33  		return int64Greater
    34  	case []uint:
    35  		return uintGreater
    36  	case []uint8:
    37  		return uint8Greater
    38  	case []uint16:
    39  		return uint16Greater
    40  	case []uint32:
    41  		return uint32Greater
    42  	case []uint64:
    43  		return uint64Greater
    44  	case []float32:
    45  		return float32Greater
    46  	case []float64:
    47  		return float64Greater
    48  	}
    49  	return nil
    50  }