Skip to content
Myr

Merge pull request #1035 from utoddl/stable-sort-doc

Before adoption: this commit came in with the history the project adopted, so no landing records it.

document that sort is stable

authored by David Lord · committed by GitHub · parents 47e5c95, 8a4a13d · Browse files · 1 file+27 added−15 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+27 added−15 removed

  • other:+27 added−15 removed

1 file+27 added−15 removed

modified:jinja2/filters.py+27 added-15 removed
278 unmodified lines
279 return sorted(value.items(), key=sort_func, reverse=reverse)280281282@environmentfilter283def 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.283def do_sort(environment, value, reverse=False, case_sensitive=False, attribute=None):284 """Sort an iterable using Python's :func:`sorted`.285289 If the iterable is made of strings the third parameter can be used to290 control the case sensitiveness of the comparison which is disabled by291 default.286 .. sourcecode:: jinja287288 {% for city in cities|sort %}289 ...290 {% endfor %}291292 :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"``.298299 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.302303 .. sourcecode:: jinja304295 {% for item in iterable|sort %}305 {% for user in users|sort(attribute="name")|sort(reverse=true, attribute="age") %}306 ...307 {% endfor %}308299 It is also possible to sort by an attribute (for example to sort300 by the date of an object) by specifying the `attribute` parameter:309 As a shortcut to chaining when the direction is the same for all310 attributes, pass a comma separate list of attributes.311312 .. sourcecode:: jinja313304 {% for item in iterable|sort(attribute='date') %}314 {% for user users|sort(attribute="age,name") %}315 ...316 {% endfor %}317318 .. versionchanged:: 2.11.0319 The ``attribute`` parameter can be a comma separated list of320 attributes, e.g. ``"age,name"``.321322 .. versionchanged:: 2.6309 The `attribute` parameter was added.310 The attribute parameter can contain multiple comma separated311 attributes, e.g. attr1,attr2.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 None
944 unmodified lines