From 42bf09f4856803fbff6b5c483b78130b600d1337 Mon Sep 17 00:00:00 2001 From: Chris Markiewicz Date: Sat, 8 Jun 2024 22:31:48 -0400 Subject: [PATCH] fix(expr): Use a sensible sorting function for sorted() Array.sort(), by default, converts numbers to strings and compares them lexically. This uses `(a > b) - (a < b)` to implement a standard comparison operator. Greater results in 1, equal in 0, lesser in -1. --- bids-validator/src/schema/expressionLanguage.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bids-validator/src/schema/expressionLanguage.ts b/bids-validator/src/schema/expressionLanguage.ts index f5e2bd1e8..56167829a 100644 --- a/bids-validator/src/schema/expressionLanguage.ts +++ b/bids-validator/src/schema/expressionLanguage.ts @@ -88,8 +88,8 @@ export const expressionFunctions = { return arg.substr(start, end - start) }, sorted: (list: T[]): T[] => { - // Copy, sort, return - return list.slice().sort() + // Use a cmp function that will work for any comparable types + return list.toSorted((a, b) => (a > b) - (a < b)) }, allequal: (a: T[], b: T[]): boolean => { return (a != null && b != null) && a.length === b.length && a.every((v, i) => v === b[i])