Skip to content

Commit

Permalink
fix(expr): Use a sensible sorting function for sorted()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
effigies committed Jun 10, 2024
1 parent 162faeb commit 42bf09f
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions bids-validator/src/schema/expressionLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ export const expressionFunctions = {
return arg.substr(start, end - start)
},
sorted: <T>(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: <T>(a: T[], b: T[]): boolean => {
return (a != null && b != null) && a.length === b.length && a.every((v, i) => v === b[i])
Expand Down

0 comments on commit 42bf09f

Please sign in to comment.