MCPcopy
hub / github.com/duke-git/lancet / compare

Function compare

internal/assert.go:160–226  ·  view source on GitHub ↗

compare x and y return : x > y -> 1, x < y -> -1, x == y -> 0, x != y -> -2

(x, y any)

Source from the content-addressed store, hash-verified

158// compare x and y return :
159// x > y -> 1, x < y -> -1, x == y -> 0, x != y -> -2
160func compare(x, y any) int {
161 vx := reflect.ValueOf(x)
162 vy := reflect.ValueOf(y)
163
164 if vx.Type() != vy.Type() {
165 return compareNotEqual
166 }
167
168 switch vx.Kind() {
169 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
170 xInt := vx.Int()
171 yInt := vy.Int()
172 if xInt > yInt {
173 return compareGreater
174 }
175 if xInt == yInt {
176 return compareEqual
177 }
178 if xInt < yInt {
179 return compareLess
180 }
181 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
182 xUint := vx.Uint()
183 yUint := vy.Uint()
184 if xUint > yUint {
185 return compareGreater
186 }
187 if xUint == yUint {
188 return compareEqual
189 }
190 if xUint < yUint {
191 return compareLess
192 }
193 case reflect.Float32, reflect.Float64:
194 xFloat := vx.Float()
195 yFloat := vy.Float()
196 if xFloat > yFloat {
197 return compareGreater
198 }
199 if xFloat == yFloat {
200 return compareEqual
201 }
202 if xFloat < yFloat {
203 return compareLess
204 }
205 case reflect.String:
206 xString := vx.String()
207 yString := vy.String()
208 if xString > yString {
209 return compareGreater
210 }
211 if xString == yString {
212 return compareEqual
213 }
214 if xString < yString {
215 return compareLess
216 }
217 default:

Callers 10

SortMethod · 0.85
SortByFieldFunction · 0.85
EqualMethod · 0.85
ShouldBeFalseMethod · 0.85
ShouldBeTrueMethod · 0.85
NotEqualMethod · 0.85
GreaterMethod · 0.85
GreaterOrEqualMethod · 0.85
LessMethod · 0.85
LessOrEqualMethod · 0.85

Calls 3

KindMethod · 0.80
StringMethod · 0.65
ValueOfMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…