{"record":{"id":"5deab5c466c9a967","repo":"antonmedv/fx","slug":"cannot-sort-typeof-x","errorCode":null,"errorMessage":"Cannot sort ${typeof x}","messagePattern":"Cannot sort (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"internal/engine/stdlib.js","lineNumber":40,"sourceCode":"  if (typeof fn === 'function') return fn(...args)\n  return fn\n}\n\nfunction len(x) {\n  if (Array.isArray(x)) return x.length\n  if (typeof x === 'string') return x.length\n  if (typeof x === 'object' && x !== null) return Object.keys(x).length\n  throw new Error(`Cannot get length of ${typeof x}`)\n}\n\nfunction uniq(x) {\n  if (Array.isArray(x)) return [...new Set(x)]\n  throw new Error(`Cannot get unique values of ${typeof x}`)\n}\n\nfunction sort(x) {\n  if (Array.isArray(x)) return x.sort()\n  throw new Error(`Cannot sort ${typeof x}`)\n}\n\nfunction isFalsely(x) {\n  return x === false || x === null || x === undefined\n}\n\nfunction filter(fn) {\n  return function (x) {\n    if (Array.isArray(x)) {\n      return x.filter((v, i) => !isFalsely(fn(v, i)))\n    }\n    return isFalsely(fn(x)) ? skip : x\n  }\n}\n\nfunction map(fn) {\n  return function (x) {\n    if (Array.isArray(x)) {","sourceCodeStart":22,"sourceCodeEnd":58,"githubUrl":"https://github.com/antonmedv/fx/blob/4f31cd3a0c5d66f1b4290a2719bab14a5cee8ebe/internal/engine/stdlib.js#L22-L58","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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 ?? [])"],"exampleFix":"// before\nconst sorted = sort(cfg)\n// after\nconst sorted = Array.isArray(cfg) ? sort(cfg) : sortKeys(cfg)","handlingStrategy":"type-guard","validationCode":"if (!Array.isArray(x)) throw new TypeError('sort() requires an array; got ' + (x === null ? 'null' : typeof x))","typeGuard":"function isSortable(x) { return Array.isArray(x) }","tryCatchPattern":"let sorted; try { sorted = sort(x) } catch (e) { if (e.message.startsWith('Cannot sort')) { sorted = [] } else { throw e } }","preventionTips":["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"],"tags":["type-error","runtime","stdlib"],"backgroundTag":"invalid-argument-value","analyzedSha":"4f31cd3a0c5d66f1b4290a2719bab14a5cee8ebe","analyzedAt":"2026-09-02T02:17:47.344Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}