11import { orderByWithFractionalIndex } from "@tanstack/db-ivm"
2- import { ascComparator , descComparator } from "../../utils/comparison.js"
2+ import { defaultComparator , makeComparator } from "../../utils/comparison.js"
33import { compileExpression } from "./evaluators.js"
44import type { OrderByClause } from "../ir.js"
55import type { NamespacedAndKeyedStream , NamespacedRow } from "../../types.js"
@@ -19,7 +19,7 @@ export function processOrderBy(
1919 // Pre-compile all order by expressions
2020 const compiledOrderBy = orderByClause . map ( ( clause ) => ( {
2121 compiledExpression : compileExpression ( clause . expression ) ,
22- direction : clause . direction ,
22+ compareOptions : clause . compareOptions ,
2323 } ) )
2424
2525 // Create a value extractor function for the orderBy operator
@@ -53,35 +53,31 @@ export function processOrderBy(
5353 }
5454
5555 // 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
7067 }
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 )
7868 }
69+ return arrayA . length - arrayB . length
70+ }
7971
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 )
8177 }
82- }
8378
84- const comparator = makeComparator ( )
79+ return defaultComparator ( a , b )
80+ }
8581
8682 // Use fractional indexing and return the tuple [value, index]
8783 return pipeline . pipe (
0 commit comments