mirror of
https://github.com/mikefarah/yq.git
synced 2026-09-01 05:34:51 +08:00
fix(sort): avoid int64 overflow comparing large integers (#2771)
The int/int comparator returned int(lhs - rhs); the int64 subtraction overflows for large-magnitude values, giving the wrong sign. Compare directly instead. Co-authored-by: max <max@example.com>
This commit is contained in:
@@ -166,7 +166,12 @@ func (a sortableNodeArray) compare(lhs *CandidateNode, rhs *CandidateNode, dateT
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return int(lhsNum - rhsNum)
|
||||
if lhsNum < rhsNum {
|
||||
return -1
|
||||
} else if lhsNum > rhsNum {
|
||||
return 1
|
||||
}
|
||||
return 0
|
||||
} else if (lhsTag == "!!int" || lhsTag == "!!float") && (rhsTag == "!!int" || rhsTag == "!!float") {
|
||||
lhsNum, err := strconv.ParseFloat(lhs.Value, 64)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user