Skip to content

Commit 4af292f

Browse files
committed
Fixed handling of nulls with sort_by expressions #2164
1 parent f238f01 commit 4af292f

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

pkg/yqlib/operator_sort.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func (a sortableNodeArray) Less(i, j int) bool {
6969
rhsContext := a[j].CompareContext
7070

7171
rhsEl := rhsContext.MatchingNodes.Front()
72+
7273
for lhsEl := lhsContext.MatchingNodes.Front(); lhsEl != nil && rhsEl != nil; lhsEl = lhsEl.Next() {
7374
lhs := lhsEl.Value.(*CandidateNode)
7475
rhs := rhsEl.Value.(*CandidateNode)
@@ -83,7 +84,7 @@ func (a sortableNodeArray) Less(i, j int) bool {
8384

8485
rhsEl = rhsEl.Next()
8586
}
86-
return false
87+
return lhsContext.MatchingNodes.Len() < rhsContext.MatchingNodes.Len()
8788
}
8889

8990
func (a sortableNodeArray) compare(lhs *CandidateNode, rhs *CandidateNode, dateTimeLayout string) int {

pkg/yqlib/operator_sort_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ var sortByOperatorScenarios = []expressionScenario{
2121
"D0, P[0], (!!map)::{a: banana}\n",
2222
},
2323
},
24+
{
25+
description: "Sort by with null",
26+
skipDoc: true,
27+
document: "[{a: banana},null,{a: apple}]",
28+
expression: `sort_by(.a)[]`,
29+
expected: []string{
30+
"D0, P[1], (!!null)::null\n",
31+
"D0, P[2], (!!map)::{a: apple}\n",
32+
"D0, P[0], (!!map)::{a: banana}\n",
33+
},
34+
},
2435
{
2536
description: "Sort by multiple fields",
2637
document: "[{a: dog},{a: cat, b: banana},{a: cat, b: apple}]",

0 commit comments

Comments
 (0)