goext/langext/compare.go

104 lines
1.2 KiB
Go
Raw Normal View History

2022-10-27 16:00:57 +02:00
package langext
func CompareIntArr(arr1 []int, arr2 []int) bool {
for i := 0; i < len(arr1) || i < len(arr2); i++ {
if i < len(arr1) && i < len(arr2) {
if arr1[i] < arr2[i] {
return true
} else if arr1[i] > arr2[i] {
return false
} else {
continue
}
}
if i < len(arr1) {
return true
} else { // if i < len(arr2)
return false
}
}
return false
}
2022-10-27 18:04:20 +02:00
2023-05-24 21:55:21 +02:00
func CompareArr[T OrderedConstraint](arr1 []T, arr2 []T) int {
2022-10-27 18:04:20 +02:00
for i := 0; i < len(arr1) || i < len(arr2); i++ {
if i < len(arr1) && i < len(arr2) {
if arr1[i] < arr2[i] {
2023-05-24 21:55:21 +02:00
return -1
2022-10-27 18:04:20 +02:00
} else if arr1[i] > arr2[i] {
2023-05-24 21:55:21 +02:00
return +2
2022-10-27 18:04:20 +02:00
} else {
continue
}
}
if i < len(arr1) {
2023-05-24 21:55:21 +02:00
return +1
2022-10-27 18:04:20 +02:00
} else { // if i < len(arr2)
2023-05-24 21:55:21 +02:00
return -1
2022-10-27 18:04:20 +02:00
}
}
2023-05-24 21:55:21 +02:00
return 0
2022-10-27 18:04:20 +02:00
}
2023-05-24 21:32:00 +02:00
func CompareString(a, b string) int {
if a == b {
return 0
}
if a < b {
return -1
}
return +1
}
func CompareInt(a, b int) int {
if a == b {
return 0
}
if a < b {
return -1
}
return +1
}
func CompareInt64(a, b int64) int {
if a == b {
return 0
}
if a < b {
return -1
}
return +1
}
func Compare[T OrderedConstraint](a, b T) int {
if a == b {
return 0
}
if a < b {
return -1
}
return +1
}