Skip to content

Commit

Permalink
Fixed handling of nulls with sort_by expressions #2164
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Nov 16, 2024
1 parent f238f01 commit 4af292f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pkg/yqlib/operator_sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func (a sortableNodeArray) Less(i, j int) bool {
rhsContext := a[j].CompareContext

rhsEl := rhsContext.MatchingNodes.Front()

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

rhsEl = rhsEl.Next()
}
return false
return lhsContext.MatchingNodes.Len() < rhsContext.MatchingNodes.Len()
}

func (a sortableNodeArray) compare(lhs *CandidateNode, rhs *CandidateNode, dateTimeLayout string) int {
Expand Down
11 changes: 11 additions & 0 deletions pkg/yqlib/operator_sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ var sortByOperatorScenarios = []expressionScenario{
"D0, P[0], (!!map)::{a: banana}\n",
},
},
{
description: "Sort by with null",
skipDoc: true,
document: "[{a: banana},null,{a: apple}]",
expression: `sort_by(.a)[]`,
expected: []string{
"D0, P[1], (!!null)::null\n",
"D0, P[2], (!!map)::{a: apple}\n",
"D0, P[0], (!!map)::{a: banana}\n",
},
},
{
description: "Sort by multiple fields",
document: "[{a: dog},{a: cat, b: banana},{a: cat, b: apple}]",
Expand Down

0 comments on commit 4af292f

Please sign in to comment.