gutil_comparator.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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/gogf/gf.
  6. package gutil
  7. import (
  8. "strings"
  9. "github.com/gogf/gf/util/gconv"
  10. )
  11. // Comparator is a function that compare a and b, and returns the result as int.
  12. //
  13. // Should return a number:
  14. // negative , if a < b
  15. // zero , if a == b
  16. // positive , if a > b
  17. type Comparator func(a, b interface{}) int
  18. // ComparatorString provides a fast comparison on strings.
  19. func ComparatorString(a, b interface{}) int {
  20. return strings.Compare(gconv.String(a), gconv.String(b))
  21. }
  22. // ComparatorInt provides a basic comparison on int.
  23. func ComparatorInt(a, b interface{}) int {
  24. return gconv.Int(a) - gconv.Int(b)
  25. }
  26. // ComparatorInt8 provides a basic comparison on int8.
  27. func ComparatorInt8(a, b interface{}) int {
  28. return int(gconv.Int8(a) - gconv.Int8(b))
  29. }
  30. // ComparatorInt16 provides a basic comparison on int16.
  31. func ComparatorInt16(a, b interface{}) int {
  32. return int(gconv.Int16(a) - gconv.Int16(b))
  33. }
  34. // ComparatorInt32 provides a basic comparison on int32.
  35. func ComparatorInt32(a, b interface{}) int {
  36. return int(gconv.Int32(a) - gconv.Int32(b))
  37. }
  38. // ComparatorInt64 provides a basic comparison on int64.
  39. func ComparatorInt64(a, b interface{}) int {
  40. return int(gconv.Int64(a) - gconv.Int64(b))
  41. }
  42. // ComparatorUint provides a basic comparison on uint.
  43. func ComparatorUint(a, b interface{}) int {
  44. return int(gconv.Uint(a) - gconv.Uint(b))
  45. }
  46. // ComparatorUint8 provides a basic comparison on uint8.
  47. func ComparatorUint8(a, b interface{}) int {
  48. return int(gconv.Uint8(a) - gconv.Uint8(b))
  49. }
  50. // ComparatorUint16 provides a basic comparison on uint16.
  51. func ComparatorUint16(a, b interface{}) int {
  52. return int(gconv.Uint16(a) - gconv.Uint16(b))
  53. }
  54. // ComparatorUint32 provides a basic comparison on uint32.
  55. func ComparatorUint32(a, b interface{}) int {
  56. return int(gconv.Uint32(a) - gconv.Uint32(b))
  57. }
  58. // ComparatorUint64 provides a basic comparison on uint64.
  59. func ComparatorUint64(a, b interface{}) int {
  60. return int(gconv.Uint64(a) - gconv.Uint64(b))
  61. }
  62. // ComparatorFloat32 provides a basic comparison on float32.
  63. func ComparatorFloat32(a, b interface{}) int {
  64. aFloat := gconv.Float32(a)
  65. bFloat := gconv.Float32(b)
  66. if aFloat == bFloat {
  67. return 0
  68. }
  69. if aFloat > bFloat {
  70. return 1
  71. }
  72. return -1
  73. }
  74. // ComparatorFloat64 provides a basic comparison on float64.
  75. func ComparatorFloat64(a, b interface{}) int {
  76. aFloat := gconv.Float64(a)
  77. bFloat := gconv.Float64(b)
  78. if aFloat == bFloat {
  79. return 0
  80. }
  81. if aFloat > bFloat {
  82. return 1
  83. }
  84. return -1
  85. }
  86. // ComparatorByte provides a basic comparison on byte.
  87. func ComparatorByte(a, b interface{}) int {
  88. return int(gconv.Byte(a) - gconv.Byte(b))
  89. }
  90. // ComparatorRune provides a basic comparison on rune.
  91. func ComparatorRune(a, b interface{}) int {
  92. return int(gconv.Rune(a) - gconv.Rune(b))
  93. }
  94. // ComparatorTime provides a basic comparison on time.Time.
  95. func ComparatorTime(a, b interface{}) int {
  96. aTime := gconv.Time(a)
  97. bTime := gconv.Time(b)
  98. switch {
  99. case aTime.After(bTime):
  100. return 1
  101. case aTime.Before(bTime):
  102. return -1
  103. default:
  104. return 0
  105. }
  106. }