github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/base/bcomparator/README.md (about)

     1  # bcomparator
     2  
     3  比较器,用于比较两个对象的大小。
     4  
     5  ## API
     6  
     7  传入a,b两个相同类型的对象,返回0,1,-1来判断a,b大小,
     8  
     9  1. a > b 返回1
    10  2. a == b 返回0
    11  3. a < b 返回-1
    12  
    13  ```go
    14  Comparator[T any] func (a, b T) int
    15  ```
    16  
    17  - OrderedComparator 返回类型[Integer | Float | ~string] 的默认比较器
    18  
    19  ```go
    20  OrderedComparator[T btype.Ordered]() Comparator[T]
    21  ```
    22  
    23  - ReverseComparator 传入比较器,将返回值反转
    24  
    25  ```go
    26  ReverseComparator[T any](comparator Comparator[T]) Comparator[T]
    27  ```
    28  
    29  - StringComparator 返回类型string的默认比较器
    30  
    31  ```go
    32  StringComparator() Comparator[string]
    33  ```
    34  
    35  - IntComparator 返回类型int的默认比较器
    36  
    37  ```go
    38  IntComparator() Comparator[int]
    39  ```
    40  
    41  - Int8Comparator 返回类型int8的默认比较器
    42  
    43  ```go
    44  Int8Comparator() Comparator[int8]
    45  ```
    46  
    47  - Int16Comparator 返回类型int16的默认比较器
    48  
    49  ```go
    50  Int16Comparator() Comparator[int16]
    51  ```
    52  
    53  - Int32Comparator 返回类型int32的默认比较器
    54  
    55  ```go
    56  Int32Comparator() Comparator[int32]
    57  ```
    58  
    59  - Int64Comparator 返回类型int64的默认比较器
    60  
    61  ```go
    62  Int64Comparator() Comparator[int64]
    63  ```
    64  
    65  - UintComparator 返回类型uint的默认比较器
    66  
    67  ```go
    68  UintComparator() Comparator[uint]
    69  ```
    70  
    71  - Uint8Comparator 返回类型uint8的默认比较器
    72  
    73  ```go
    74  Uint8Comparator() Comparator[uint8]
    75  ```
    76  
    77  - Uint16Comparator 返回类型uint16的默认比较器
    78  
    79  ```go
    80  Uint16Comparator() Comparator[uint16]
    81  ```
    82  
    83  - Uint32Comparator 返回类型uint32的默认比较器
    84  
    85  ```go
    86  Uint32Comparator() Comparator[uint32]
    87  ```
    88  
    89  - Uint64Comparator 返回类型uint64的默认比较器
    90  
    91  ```go
    92  Uint64Comparator() Comparator[uint64]
    93  ```
    94  
    95  - Float32Comparator 返回类型float32的默认比较器
    96  
    97  ```go
    98  Float32Comparator() Comparator[float32]
    99  ```
   100  
   101  - Float64Comparator 返回类型float64的默认比较器
   102  
   103  ```go
   104  Float64Comparator() Comparator[float64]
   105  ```
   106  
   107  - BoolComparator 返回类型bool的默认比较器
   108  
   109  ```go
   110  BoolComparator() Comparator[bool]
   111  ```
   112  
   113  ## EXAMPLE
   114  
   115  ```go
   116  package main
   117  
   118  import (
   119  	"fmt"
   120  	"github.com/songzhibin97/go-baseutils/base/bcomparator"
   121  )
   122  
   123  func main() {
   124  	fmt.Println(bcomparator.OrderedComparator[int]()(1, 2)) // -1
   125  	fmt.Println(bcomparator.OrderedComparator[int]()(2, 2)) // 0
   126  	fmt.Println(bcomparator.OrderedComparator[int]()(3, 2)) // 1
   127  }
   128  ```