clean up sort filter docs
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 996bcad · Browse files · 1 file·+22 added−21 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+22 added−21 removed
- other:+22 added−21 removed
Provenance
- Commit
8a4a13d- Tree
f3e050b- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
996bcad
1 file changed+22 added−21 removed
modified:jinja2/filters.py+22 added-21 removed
278 unmodified lines279 return sorted(value.items(), key=sort_func, reverse=reverse)280 281 282 @environmentfilter283-def do_sort(284- environment, value, reverse=False, case_sensitive=False, attribute=None285-):286- """Sort an iterable. Per default it sorts ascending, if you pass it287- true as first argument it will reverse the sorting. The sort is stable,288- i.e. it guarantees not to change the relative order of elements that289- compare equal.283+def do_sort(environment, value, reverse=False, case_sensitive=False, attribute=None):284+ """Sort an iterable using Python's :func:`sorted`.285 291- If the iterable is made of strings the third parameter can be used to292- control the case sensitiveness of the comparison which is disabled by293- default.294-286 .. sourcecode:: jinja287 297- {% for item in iterable|sort %}288+ {% for city in cities|sort %}289 ...290 {% endfor %}291 301- It is also possible to sort by an attribute (for example to sort302- by the date of an object) by specifying the `attribute` parameter:292+ :param reverse: Sort descending instead of ascending.293+ :param case_sensitive: When sorting strings, sort upper and lower294+ case separately.295+ :param attribute: When sorting objects or dicts, an attribute or296+ key to sort by. Can use dot notation like ``"address.city"``.297+ Can be a list of attributes like ``"age,name"``.298 299+ The sort is stable, it does not change the relative order of300+ elements that compare equal. This makes it is possible to chain301+ sorts on different attributes and ordering.302+303 .. sourcecode:: jinja304 306- {% for item in iterable|sort(attribute='date') %}305+ {% for user in users|sort(attribute="name")|sort(reverse=true, attribute="age") %}306 ...307 {% endfor %}308 310- Because the sort is stable, it is possible to chain sorts on different311- attributes and ordering:309+ As a shortcut to chaining when the direction is the same for all310+ attributes, pass a comma separate list of attributes.311 312 .. sourcecode:: jinja313 315- {% for item in iterable|sort(attribute='name')|sort(true,attribute='date') %}314+ {% for user users|sort(attribute="age,name") %}315 ...316 {% endfor %}317 318+ .. versionchanged:: 2.11.0319+ The ``attribute`` parameter can be a comma separated list of320+ attributes, e.g. ``"age,name"``.321+322 .. versionchanged:: 2.6320- The `attribute` parameter was added.321- The attribute parameter can contain multiple comma separated322- attributes, e.g. 'attr1,attr2', all subject to the same ordering.323+ The ``attribute`` parameter was added.324 """325 key_func = make_multi_attrgetter(326 environment, attribute,327 postprocess=ignore_case if not case_sensitive else None944 unmodified lines