github.com/wangyougui/gf/v2@v2.6.5/util/gutil/gutil_comparator.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/wangyougui/gf.
     6  
     7  package gutil
     8  
     9  import (
    10  	"strings"
    11  
    12  	"github.com/wangyougui/gf/v2/util/gconv"
    13  )
    14  
    15  // Comparator is a function that compare a and b, and returns the result as int.
    16  //
    17  // Should return a number:
    18  //
    19  //	negative , if a < b
    20  //	zero     , if a == b
    21  //	positive , if a > b
    22  type Comparator func(a, b interface{}) int
    23  
    24  // ComparatorString provides a fast comparison on strings.
    25  func ComparatorString(a, b interface{}) int {
    26  	return strings.Compare(gconv.String(a), gconv.String(b))
    27  }
    28  
    29  // ComparatorInt provides a basic comparison on int.
    30  func ComparatorInt(a, b interface{}) int {
    31  	return gconv.Int(a) - gconv.Int(b)
    32  }
    33  
    34  // ComparatorInt8 provides a basic comparison on int8.
    35  func ComparatorInt8(a, b interface{}) int {
    36  	return int(gconv.Int8(a) - gconv.Int8(b))
    37  }
    38  
    39  // ComparatorInt16 provides a basic comparison on int16.
    40  func ComparatorInt16(a, b interface{}) int {
    41  	return int(gconv.Int16(a) - gconv.Int16(b))
    42  }
    43  
    44  // ComparatorInt32 provides a basic comparison on int32.
    45  func ComparatorInt32(a, b interface{}) int {
    46  	return int(gconv.Int32(a) - gconv.Int32(b))
    47  }
    48  
    49  // ComparatorInt64 provides a basic comparison on int64.
    50  func ComparatorInt64(a, b interface{}) int {
    51  	return int(gconv.Int64(a) - gconv.Int64(b))
    52  }
    53  
    54  // ComparatorUint provides a basic comparison on uint.
    55  func ComparatorUint(a, b interface{}) int {
    56  	return int(gconv.Uint(a) - gconv.Uint(b))
    57  }
    58  
    59  // ComparatorUint8 provides a basic comparison on uint8.
    60  func ComparatorUint8(a, b interface{}) int {
    61  	return int(gconv.Uint8(a) - gconv.Uint8(b))
    62  }
    63  
    64  // ComparatorUint16 provides a basic comparison on uint16.
    65  func ComparatorUint16(a, b interface{}) int {
    66  	return int(gconv.Uint16(a) - gconv.Uint16(b))
    67  }
    68  
    69  // ComparatorUint32 provides a basic comparison on uint32.
    70  func ComparatorUint32(a, b interface{}) int {
    71  	return int(gconv.Uint32(a) - gconv.Uint32(b))
    72  }
    73  
    74  // ComparatorUint64 provides a basic comparison on uint64.
    75  func ComparatorUint64(a, b interface{}) int {
    76  	return int(gconv.Uint64(a) - gconv.Uint64(b))
    77  }
    78  
    79  // ComparatorFloat32 provides a basic comparison on float32.
    80  func ComparatorFloat32(a, b interface{}) int {
    81  	aFloat := gconv.Float32(a)
    82  	bFloat := gconv.Float32(b)
    83  	if aFloat == bFloat {
    84  		return 0
    85  	}
    86  	if aFloat > bFloat {
    87  		return 1
    88  	}
    89  	return -1
    90  }
    91  
    92  // ComparatorFloat64 provides a basic comparison on float64.
    93  func ComparatorFloat64(a, b interface{}) int {
    94  	aFloat := gconv.Float64(a)
    95  	bFloat := gconv.Float64(b)
    96  	if aFloat == bFloat {
    97  		return 0
    98  	}
    99  	if aFloat > bFloat {
   100  		return 1
   101  	}
   102  	return -1
   103  }
   104  
   105  // ComparatorByte provides a basic comparison on byte.
   106  func ComparatorByte(a, b interface{}) int {
   107  	return int(gconv.Byte(a) - gconv.Byte(b))
   108  }
   109  
   110  // ComparatorRune provides a basic comparison on rune.
   111  func ComparatorRune(a, b interface{}) int {
   112  	return int(gconv.Rune(a) - gconv.Rune(b))
   113  }
   114  
   115  // ComparatorTime provides a basic comparison on time.Time.
   116  func ComparatorTime(a, b interface{}) int {
   117  	aTime := gconv.Time(a)
   118  	bTime := gconv.Time(b)
   119  	switch {
   120  	case aTime.After(bTime):
   121  		return 1
   122  	case aTime.Before(bTime):
   123  		return -1
   124  	default:
   125  		return 0
   126  	}
   127  }