# Sort with two attributes

**URL:** <https://forum.ansible.com/t/sort-with-two-attributes/40437>\
**Category:** Get Help\
**Tags:** ansible-core\
**Created:** [February 21, 2025, 6:07pm UTC](https://forum.ansible.com/t/sort-with-two-attributes/40437 "2025-02-21T18:07:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luiz\_Gustavo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/luiz_gustavo/32/4846_2.png) [@Luiz\_Gustavo](https://forum.ansible.com/u/Luiz_Gustavo)\
**Post date:** [February 21, 2025, 6:07pm UTC](https://forum.ansible.com/t/sort-with-two-attributes/40437/1 "2025-02-21T18:07:40Z")

</div>

Hello experts,

How to sort a dict with two attributes for output in a report, similar to the code:

```auto
---
- hosts: localhost
  gather_facts: false
  vars:

    databases: [
              {
                   "dbname": "A_dg", 
                   "is_standby": false
              }, 
              {
                  "dbname": "C_dg", 
                  "is_standby": true
              },
             {
                  "dbname": "B_dg", 
                  "is_standby": true
              }
            ]

  tasks:

    - name: "Output"
      debug:
        msg: |
          {% for db in databases | sort(attribute='is_standby', reverse=true) | sort(attribute='dbname') %}
          {{ db.dbname }}
          {% endfor %}

```

expected output:

```auto
TASK [Exibe bancos_standby] **********************************************************************************************************************
ok: [localhost] => {
    "msg": "B_dg\nC_dg\nA_dg\n"
}

```

But I can only sort by the second field:

```auto
TASK [Exibe bancos] ******************************************************************************************************************************
ok: [localhost] => {
    "msg": "A_dg\nB_dg\nC_dg\n"
}

```

I’ve tried using some of the methods below but it gave an error:

FAILED! =\> {“msg”: "The task includes an option with an undefined variable. The error was: ‘dict object’ has no attribute ‘[is\_standby,dbname]’

```auto
...
{% for db in databases | sort(attribute='is_standby,dbname', reverse=true) %}
...

```

ansible --version: ansible 2.9.27

---

<div class="post-metadata">

**Author:** ![hugonz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hugonz/32/503_2.png) [@hugonz](https://forum.ansible.com/u/hugonz)\
**Post date:** [February 21, 2025, 6:42pm UTC](https://forum.ansible.com/t/sort-with-two-attributes/40437/2 "2025-02-21T18:42:57Z")

</div>

Hi,

Sorting on a list of attributes was added to Jinja in (Jinja)version 2.11.0 per [Template Designer Documentation — Jinja Documentation (3.1.x)](https://jinja.palletsprojects.com/en/stable/templates/#jinja-filters.sort)

What version of Jinja is Ansible using? `ansible --version` gives you that. As I read the docs, what you’re doing should work, but your ansible-core is very old.

---

<div class="post-metadata">

**Author:** ![Luiz\_Gustavo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/luiz_gustavo/32/4846_2.png) [@Luiz\_Gustavo](https://forum.ansible.com/u/Luiz_Gustavo)\
**Post date:** [February 21, 2025, 7:05pm UTC](https://forum.ansible.com/t/sort-with-two-attributes/40437/3 "2025-02-21T19:05:55Z")

</div>

Hi Hugo, thanks for the reply. My ansible is ansible 2.9.27.

I think it’s unlikely that the people responsible where I work will update the version.

---

<div class="post-metadata">

**Author:** ![hugonz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hugonz/32/503_2.png) [@hugonz](https://forum.ansible.com/u/hugonz)\
**Post date:** [February 21, 2025, 7:44pm UTC](https://forum.ansible.com/t/sort-with-two-attributes/40437/4 "2025-02-21T19:44:11Z")

</div>

BTW, in newer ansible (ansible [core 2.16.14]) I tried your sorting keys and I’m getting this, which is not what you said you needed anyway

```yaml
- hosts: localhost
  gather_facts: false
  vars:
    databases: [
            {
              "dbname": "A_dg",
              "is_standby": false
            },
            {
              "dbname": "C_dg",
              "is_standby": true
            },
            {
              "dbname": "B_dg",
              "is_standby": true
            }
            ]

  tasks:
    - name: "Output"
      ansible.builtin.debug:
        msg: |
          {% for db in databases | sort(attribute='is_standby,dbname', reverse=true) %}
          {{ db.dbname }}
          {% endfor %}

```

result:

```plaintext
$ ansible-playbook doublesortdic.yml 
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does
not match 'all'

PLAY [localhost] ********************************************************************************************

TASK [Output] ***********************************************************************************************
ok: [localhost] => {
    "msg": "C_dg\nB_dg\nA_dg\n"
}

PLAY RECAP **************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0 

```

---

<div class="post-metadata">

**Author:** ![hugonz](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/hugonz/32/503_2.png) [@hugonz](https://forum.ansible.com/u/hugonz)\
**Post date:** [February 21, 2025, 7:54pm UTC](https://forum.ansible.com/t/sort-with-two-attributes/40437/5 "2025-02-21T19:54:20Z")

</div>

What you need it is to split into two lists based on true/false and _then_ sort them in forward order:

```yaml
    - name: "Output"
      ansible.builtin.debug:
        msg: |
          {% for db in databases | selectattr('is_standby','true') | sort(attribute='dbname') %}
          {{ db.dbname }}
          {% endfor %}

          {% for db in databases | selectattr('is_standby','false') | sort(attribute='dbname') %}
          {{ db.dbname }}
          {% endfor %}

```

Results

```auto
TASK [Output] ***********************************************************************************************
ok: [localhost] => {
    "msg": "B_dg\nC_dg\n\nA_dg\n"
}

PLAY RECAP **************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0   

```

I wouldn’t off the top of my head know how sorting true/false works. I wouln’ t use sort when there’s only two options.

---

<div class="post-metadata">

**Author:** ![Luiz\_Gustavo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/luiz_gustavo/32/4846_2.png) [@Luiz\_Gustavo](https://forum.ansible.com/u/Luiz_Gustavo)\
**Post date:** [February 21, 2025, 8:13pm UTC](https://forum.ansible.com/t/sort-with-two-attributes/40437/6 "2025-02-21T20:13:21Z")

</div>

Great idea! Thank you very much.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [March 23, 2025, 8:14pm UTC](https://forum.ansible.com/t/sort-with-two-attributes/40437/7 "2025-03-23T20:14:00Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
