You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[jinja] Implement sort filter with parameters (#1852)
- [x] Explore repository structure and understand codebase
- [x] Implement enhanced sort filter with `reverse`, `case_sensitive`,
and `attribute` parameters
- [x] Add comprehensive tests for the new sort filter functionality
- [x] Add dot notation support to map filter
- [x] Fix reverse filter mutation issue
- [x] Extract shared getAttributeValue helper function
- [x] Add positional argument support for sort filter (matching Jinja2
API)
- [x] Add unit tests for positional arguments
- [x] Consolidate sort and dictsort with shared compareRuntimeValues
helper
- [x] Handle edge cases: null/null, undefined/undefined, and bool/number
comparisons
- [x] Fix tojson/string filter handling of undefined values
- [x] Fix direct output `{{ value }}` to use proper JSON-like formatting
for arrays and objects
- [x] Consolidate `runtimeValueToString` and `toJSON` into a single
helper function
- [x] Run tests to validate implementation (515 passed, 1 network error
expected)
- [x] Run linting to ensure code quality
## Summary
Created a shared `compareRuntimeValues` helper function that:
- Allows `None` values to be compared with `None` values (they are
equal)
- Allows `Undefined` values to be compared with `Undefined` values (they
are equal)
- Allows booleans to be compared with numbers (false=0, true=1)
- Allows integers and floats to be compared (both are numeric)
- Throws errors for incompatible type comparisons
- Handles case_sensitive option for string comparisons
- Returns -1, 0, or 1 for use with sort functions
Both `sort` and `dictsort` filters now use this shared helper for
consistent comparison behavior.
### Output formatting:
- Consolidated `runtimeValueToString` and `toJSON` into a single
`toJSON` helper function
- `toJSON(value, null, 0, false)` - outputs "undefined" for
UndefinedValue (for direct output)
- `toJSON(value)` - outputs "null" for UndefinedValue (for JSON
serialization)
- `ArrayValue` and `ObjectValue` override `toString()` to use
`toJSON(this, null, 0, false)`
### Edge cases supported:
- `{{ [None, None] | sort }}` → `[null, null]`
- `{{ [a, b, c] | sort }}` (undefined vars) → `[undefined, undefined,
undefined]`
- `{{ [0, true, false, 1, 0.5] | sort }}` → `[0, false, 0.5, true, 1]`
<!-- START COPILOT CODING AGENT SUFFIX -->
<details>
<summary>Original prompt</summary>
> Improve and fulfil the implementation of the sort filter for arrays,
allowing you to pass various optional arguments, like `attribute`.
>
> Here is the documentation for the sort filter:
> ```
> jinja-filters.sort(value: 't.Iterable[V]', reverse: bool = False,
case_sensitive: bool = False, attribute: str | int | NoneType = None) →
't.List[V]'
> Sort an iterable using Python’s sorted().
>
> {% for city in cities|sort %}
> ...
> {% endfor %}
> Parameters:
> reverse – Sort descending instead of ascending.
>
> case_sensitive – When sorting strings, sort upper and lower case
separately.
>
> attribute – When sorting objects or dicts, an attribute or key to sort
by. Can use dot notation like "address.city". Can be a list of
attributes like "age,name".
>
> The sort is stable, it does not change the relative order of elements
that compare equal. This makes it is possible to chain sorts on
different attributes and ordering.
>
> {% for user in users|sort(attribute="name")
> |sort(reverse=true, attribute="age") %}
> ...
> {% endfor %}
> As a shortcut to chaining when the direction is the same for all
attributes, pass a comma separate list of attributes.
>
> {% for user in users|sort(attribute="age,name") %}
> ...
> {% endfor %}
> ```
>
> Be sure to add tests for your implementation.
</details>
<!-- START COPILOT CODING AGENT TIPS -->
---
✨ Let Copilot coding agent [set things up for
you](https://github.com/huggingface/huggingface.js/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot)
— coding agent works faster and does higher quality work when set up for
your repo.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: xenova <26504141+xenova@users.noreply.github.com>
Co-authored-by: Joshua Lochner <admin@xenova.com>
0 commit comments