{"record":{"id":"4c08a5c9cebeb7dd","repo":"twigphp/Twig","slug":"the-sort-filter-expects-a-sequence-or-a-mapping-got-s","errorCode":null,"errorMessage":"The \"sort\" filter expects a sequence or a mapping, got \"%s\".","messagePattern":"The \"sort\" filter expects a sequence or a mapping, got \"(.+?)\"\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"src/Extension/CoreExtension.php","lineNumber":1065,"sourceCode":"        }\n\n        return $item;\n    }\n\n    /**\n     * Sorts an array.\n     *\n     * @param array|\\Traversable $array\n     * @param ?\\Closure          $arrow\n     *\n     * @internal\n     */\n    public static function sort(Environment $env, bool $isSandboxed, $array, $arrow = null): array\n    {\n        if ($array instanceof \\Traversable) {\n            $array = iterator_to_array($array);\n        } elseif (!\\is_array($array)) {\n            throw new RuntimeError(\\sprintf('The \"sort\" filter expects a sequence or a mapping, got \"%s\".', get_debug_type($array)));\n        }\n\n        if (null !== $arrow) {\n            self::checkArrow($isSandboxed, $arrow, 'sort', 'filter');\n\n            uasort($array, $arrow);\n        } else {\n            asort($array);\n        }\n\n        return $array;\n    }\n\n    /**\n     * @internal\n     */\n    public static function inFilter($value, $compare)\n    {","sourceCodeStart":1047,"sourceCodeEnd":1083,"githubUrl":"https://github.com/twigphp/Twig/blob/a414c3a491defb5a60f2fc88ef79ff37c90010cd/src/Extension/CoreExtension.php#L1047-L1083","documentation":"The Twig `sort` filter (CoreExtension::sort) accepts an array or Traversable; Traversables are converted with iterator_to_array, but any other type (scalar, null, non-Traversable object) triggers a RuntimeError stating the actual debug type. This keeps uasort from receiving an invalid argument.","triggerScenarios":"{{ list|sort }} where list is null, a string, an integer, or a plain object (stdClass); calling sort via getAttribute on a value that turned out not to be a collection; chaining filters where an earlier filter returned a scalar or null.","commonSituations":"Sorting the result of a query that returned null on no rows; sorting a JSON-decoded object; forgetting that a variable was reassigned to a scalar upstream in the template.","solutions":["Ensure the value is an array or Traversable before sorting; dump it to verify.","Default empty cases: {{ (items|default([]))|sort }}.","Convert Traversable-to-array explicitly if needed (iterator_to_array in PHP, or ensure the data source returns arrays).","Fix the upstream source so it returns a collection instead of null/scalar."],"exampleFix":"// before (template)\n{{ userNames|sort }}\n\n// after\n{{ (userNames|default([]))|sort }}","handlingStrategy":"type-guard","validationCode":"// Twig template\n{% if items is iterable %}{{ items|sort }}{% endif %}","typeGuard":"function isSortable($v): bool { return \\is_array($v) || $v instanceof \\Traversable; }","tryCatchPattern":"try {\n    $out = \\Twig\\Extension\\CoreExtension::sort($env, false, $array, $arrow);\n} catch (\\Twig\\Error\\RuntimeError $e) {\n    $out = []; // or log/rethrow\n}","preventionTips":["Guard with |default([]) when the collection may be absent.","Convert query/collection results to arrays before passing into templates.","Keep template variables' types stable across refactors; check with dump() when unsure."],"tags":["twig","filter","type-error","sort"],"backgroundTag":"type-mismatch","analyzedSha":"a414c3a491defb5a60f2fc88ef79ff37c90010cd","analyzedAt":"2026-09-13T15:10:46.849Z","contentChangedAt":"2026-09-13T15:10:46.849Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}