{"record":{"id":"f0045810eb3cd198","repo":"nodejs/node","slug":"you-can-only-sort-by-either-key-or-value","errorCode":null,"errorMessage":"You can only sort by either \"key\" or \"value\"","messagePattern":"You can only sort by either \"key\" or \"value\"","errorType":"exception","errorClass":"FilterArgumentError","httpStatus":null,"severity":"error","filePath":"tools/inspector_protocol/jinja2/filters.py","lineNumber":230,"sourceCode":"\n        {% for item in mydict|dictsort %}\n            sort the dict by key, case insensitive\n\n        {% for item in mydict|dictsort(reverse=true) %}\n            sort the dict by key, case insensitive, reverse order\n\n        {% for item in mydict|dictsort(true) %}\n            sort the dict by key, case sensitive\n\n        {% for item in mydict|dictsort(false, 'value') %}\n            sort the dict by value, case insensitive\n    \"\"\"\n    if by == 'key':\n        pos = 0\n    elif by == 'value':\n        pos = 1\n    else:\n        raise FilterArgumentError(\n            'You can only sort by either \"key\" or \"value\"'\n        )\n\n    def sort_func(item):\n        value = item[pos]\n\n        if not case_sensitive:\n            value = ignore_case(value)\n\n        return value\n\n    return sorted(value.items(), key=sort_func, reverse=reverse)\n\n\n@environmentfilter\ndef do_sort(\n    environment, value, reverse=False, case_sensitive=False, attribute=None\n):","sourceCodeStart":212,"sourceCodeEnd":248,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/tools/inspector_protocol/jinja2/filters.py#L212-L248","documentation":"The dictsort filter sorts the items of a mapping. Its `by` argument selects the sort key: 'key' (pos 0) or 'value' (pos 1). Any other string is meaningless to the filter and is rejected with FilterArgumentError.","triggerScenarios":"Invoking {{ mydict|dictsort(case_sensitive, by) }} where `by` is anything other than the literal strings 'key' or 'value' (e.g. 'name', 'field', 'attribute').","commonSituations":"Confusing dictsort with other filters (attr/map) that take an attribute name; copy-pasting a sort field name from another part of the template.","solutions":["Use by='key' (the default) or by='value' in the dictsort call.","If you need to sort by a nested field, first map items to that field then sort, instead of passing the field name to dictsort."],"exampleFix":"{# before #}\n{{ mydict|dictsort(false, 'name') }}\n\n{# after #}\n{{ mydict|dictsort(false, 'value') }}","handlingStrategy":"validation","validationCode":"ALLOWED = ('key', 'value')\nif by not in ALLOWED:\n    raise ValueError(\"dictsort `by` must be one of %r\" % (ALLOWED,))","typeGuard":"def is_dictsort_by(value: str) -> bool:\n    return value in ('key', 'value')","tryCatchPattern":"from jinja2.exceptions import FilterArgumentError\ntry:\n    out = env.call_filter('dictsort', value, args=(False, by))\nexcept FilterArgumentError:\n    by = 'key'\n    out = env.call_filter('dictsort', value, args=(False, by))","preventionTips":["Whitelist the `by` argument in any code that builds dictsort dynamically.","Keep dictsort and attribute-based filters (map/selectattr) distinct in templates."],"tags":["jinja2","filter","dictsort","validation"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}