1
1
import { orderByWithFractionalIndex } from "@tanstack/db-ivm"
2
- import { ascComparator , descComparator } from "../../utils/comparison.js"
2
+ import { defaultComparator , makeComparator } from "../../utils/comparison.js"
3
3
import { compileExpression } from "./evaluators.js"
4
4
import type { OrderByClause } from "../ir.js"
5
5
import type { NamespacedAndKeyedStream , NamespacedRow } from "../../types.js"
@@ -19,7 +19,7 @@ export function processOrderBy(
19
19
// Pre-compile all order by expressions
20
20
const compiledOrderBy = orderByClause . map ( ( clause ) => ( {
21
21
compiledExpression : compileExpression ( clause . expression ) ,
22
- direction : clause . direction ,
22
+ compareOptions : clause . compareOptions ,
23
23
} ) )
24
24
25
25
// Create a value extractor function for the orderBy operator
@@ -53,35 +53,31 @@ export function processOrderBy(
53
53
}
54
54
55
55
// Create a multi-property comparator that respects the order and direction of each property
56
- const makeComparator = ( ) => {
57
- return ( a : unknown , b : unknown ) => {
58
- // If we're comparing arrays (multiple properties), compare each property in order
59
- if ( orderByClause . length > 1 ) {
60
- const arrayA = a as Array < unknown >
61
- const arrayB = b as Array < unknown >
62
- for ( let i = 0 ; i < orderByClause . length ; i ++ ) {
63
- const direction = orderByClause [ i ] ! . direction
64
- const compareFn =
65
- direction === `desc` ? descComparator : ascComparator
66
- const result = compareFn ( arrayA [ i ] , arrayB [ i ] )
67
- if ( result !== 0 ) {
68
- return result
69
- }
56
+ const comparator = ( a : unknown , b : unknown ) => {
57
+ // If we're comparing arrays (multiple properties), compare each property in order
58
+ if ( orderByClause . length > 1 ) {
59
+ const arrayA = a as Array < unknown >
60
+ const arrayB = b as Array < unknown >
61
+ for ( let i = 0 ; i < orderByClause . length ; i ++ ) {
62
+ const clause = orderByClause [ i ] !
63
+ const compareFn = makeComparator ( clause . compareOptions )
64
+ const result = compareFn ( arrayA [ i ] , arrayB [ i ] )
65
+ if ( result !== 0 ) {
66
+ return result
70
67
}
71
- return arrayA . length - arrayB . length
72
- }
73
-
74
- // Single property comparison
75
- if ( orderByClause . length === 1 ) {
76
- const direction = orderByClause [ 0 ] ! . direction
77
- return direction === `desc` ? descComparator ( a , b ) : ascComparator ( a , b )
78
68
}
69
+ return arrayA . length - arrayB . length
70
+ }
79
71
80
- return ascComparator ( a , b )
72
+ // Single property comparison
73
+ if ( orderByClause . length === 1 ) {
74
+ const clause = orderByClause [ 0 ] !
75
+ const compareFn = makeComparator ( clause . compareOptions )
76
+ return compareFn ( a , b )
81
77
}
82
- }
83
78
84
- const comparator = makeComparator ( )
79
+ return defaultComparator ( a , b )
80
+ }
85
81
86
82
// Use fractional indexing and return the tuple [value, index]
87
83
return pipeline . pipe (
0 commit comments