Skip to content

Instantly share code, notes, and snippets.

@primaryobjects
Last active September 17, 2024 00:58
Show Gist options
  • Save primaryobjects/ab07cc7cac7e6480175fc995c022a7fc to your computer and use it in GitHub Desktop.
Save primaryobjects/ab07cc7cac7e6480175fc995c022a7fc to your computer and use it in GitHub Desktop.
Jinja syntax to add a new property to sort a list by
{% set updated_users = [] %}
{% for user in all_users %}
{% set names = user.full_name.split() %}
{% set first_name = names[0] %}
{% set last_name = names[1:] | join(' ') %}
{% set updated_user = user.copy() %}
{% set _ = updated_user.update({'first_name': first_name, 'last_name': last_name}) %}
{% set _ = updated_users.append(updated_user) %}
{% endfor %}
{% set sorted_users = updated_users | sort(attribute='last_name') %}
{% for user in sorted_users %}
{{ user.first_name }} {{ user.last_name }}
{% endfor %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment