Skip to content

Commit

Permalink
Make sort stable (#416)
Browse files Browse the repository at this point in the history
Sort reverses records that compare as equal, making it unstable.  Fix
that by using the proper comparison in expr.(*RecordSlice).Less.

This reverts #37.
  • Loading branch information
nwt committed Mar 12, 2020
1 parent 98b50b0 commit a4b7935
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 24 deletions.
2 changes: 1 addition & 1 deletion expr/sort.go
Expand Up @@ -103,7 +103,7 @@ func (r *RecordSlice) Swap(i, j int) { r.records[i], r.records[j] = r.records[j]

// Less implements sort.Interface for *Record slices.
func (r *RecordSlice) Less(i, j int) bool {
return r.sorter(r.records[i], r.records[j]) <= 0
return r.sorter(r.records[i], r.records[j]) < 0
}

// Push adds x as element Len(). Implements heap.Interface.
Expand Down
2 changes: 1 addition & 1 deletion tests/suite/sort/sort-4-1.yaml
Expand Up @@ -14,6 +14,6 @@ output: |
0:[a;]
0:[b;]
0:[c;]
0:[-;]
#1:record[notS:string]
1:[bleah;]
0:[-;]
14 changes: 7 additions & 7 deletions tests/suite/sort/sort-4-2.yaml
Expand Up @@ -10,10 +10,10 @@ input: |
1:[bleah;]
output: |
#0:record[notS:string]
0:[bleah;]
#1:record[s:string]
1:[-;]
1:[a;]
1:[b;]
1:[c;]
#0:record[s:string]
0:[-;]
#1:record[notS:string]
1:[bleah;]
0:[a;]
0:[b;]
0:[c;]
2 changes: 1 addition & 1 deletion tests/suite/sort/sort-4-3.yaml
Expand Up @@ -14,6 +14,6 @@ output: |
0:[c;]
0:[b;]
0:[a;]
0:[-;]
#1:record[notS:string]
1:[bleah;]
0:[-;]
14 changes: 7 additions & 7 deletions tests/suite/sort/sort-4-4.yaml
Expand Up @@ -10,10 +10,10 @@ input: |
1:[bleah;]
output: |
#0:record[notS:string]
0:[bleah;]
#1:record[s:string]
1:[-;]
1:[c;]
1:[b;]
1:[a;]
#0:record[s:string]
0:[-;]
#1:record[notS:string]
1:[bleah;]
0:[c;]
0:[b;]
0:[a;]

0 comments on commit a4b7935

Please sign in to comment.