antonmedv/fx · error · Error
Cannot sort ${typeof x}
Error message
Cannot sort ${typeof x} What it means
sort(x) sorts in place and only accepts arrays; any other type throws 'Cannot sort <type>'. Strings, objects, numbers, null and undefined are all rejected — the library makes you convert explicitly rather than guessing a comparison order.
Source
Thrown at internal/engine/stdlib.js:40
if (typeof fn === 'function') return fn(...args)
return fn
}
function len(x) {
if (Array.isArray(x)) return x.length
if (typeof x === 'string') return x.length
if (typeof x === 'object' && x !== null) return Object.keys(x).length
throw new Error(`Cannot get length of ${typeof x}`)
}
function uniq(x) {
if (Array.isArray(x)) return [...new Set(x)]
throw new Error(`Cannot get unique values of ${typeof x}`)
}
function sort(x) {
if (Array.isArray(x)) return x.sort()
throw new Error(`Cannot sort ${typeof x}`)
}
function isFalsely(x) {
return x === false || x === null || x === undefined
}
function filter(fn) {
return function (x) {
if (Array.isArray(x)) {
return x.filter((v, i) => !isFalsely(fn(v, i)))
}
return isFalsely(fn(x)) ? skip : x
}
}
function map(fn) {
return function (x) {
if (Array.isArray(x)) {View on GitHub (pinned to 4f31cd3a0c)
Solutions
- Verify with Array.isArray(x) before sorting and handle the non-array branch
- For objects, sort keys instead: Object.keys(x).sort() or the library's sortKeys(x)
- For strings, split into characters first: x.split('').sort().join('')
- Default nullable values: sort(x ?? [])
Example fix
// before const sorted = sort(cfg) // after const sorted = Array.isArray(cfg) ? sort(cfg) : sortKeys(cfg)
Defensive patterns
Strategy: type-guard
Validate before calling
if (!Array.isArray(x)) throw new TypeError('sort() requires an array; got ' + (x === null ? 'null' : typeof x)) Type guard
function isSortable(x) { return Array.isArray(x) } Try / catch
let sorted; try { sorted = sort(x) } catch (e) { if (e.message.startsWith('Cannot sort')) { sorted = [] } else { throw e } } Prevention
- Use sortKeys for objects instead of sort
- Default nullable values: sort(x ?? [])
- Sort strings via split('') if character order matters
- Validate array-ness right after parsing external data
When it happens
Trigger: sort('cba'), sort({a:1, b:2}), sort(null), sort(undefined), or sort applied to a pipeline value that is a scalar/object rather than an array.
Common situations: Sorting object keys by accident (need Object.keys(obj).sort() or sortKeys), sorting a string's characters (need split('') first), or sorting data that an upstream parse returned as null.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Cannot get length of ${typeof x}
- Cannot get unique values of ${typeof x}
- Cannot flatten ${typeof x}
- Cannot reverse ${typeof x}
- Cannot get keys of ${typeof x}
AI-assisted analysis of antonmedv/fx@4f31cd3a0c (2026-09-02).
Data as JSON: /api/errors/5deab5c466c9a967.
Report an issue: GitHub.