# Sorting dicts in lists - works different between my machine and others

**URL:** <https://forum.ansible.com/t/sorting-dicts-in-lists-works-different-between-my-machine-and-others/30329>\
**Category:** Ansible Project\
**Tags:** awx\
**Created:** [May 14, 2019, 3:40pm UTC](https://forum.ansible.com/t/sorting-dicts-in-lists-works-different-between-my-machine-and-others/30329 "2019-05-14T15:40:22Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![JonTheNiceGuy](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jontheniceguy/32/2267_2.png) [@JonTheNiceGuy](https://forum.ansible.com/u/JonTheNiceGuy)\
**Post date:** [May 14, 2019, 3:40pm UTC](https://forum.ansible.com/t/sorting-dicts-in-lists-works-different-between-my-machine-and-others/30329/1 "2019-05-14T15:40:22Z")

</div>

Hi,

I have a variable which is a list (sourced from registering the response from a 3rd party module). In that list, each item is a dict, as follows:

[  
{‘policyid’: ‘1009’, ‘other’: ‘content’},  
{‘policyid’: ‘2019’, ‘other’: ‘content’},  
{‘policyid’: ‘1024’, ‘other’: ‘content’},  
{‘policyid’: ‘1000’, ‘other’: ‘content’},  
{‘policyid’: ‘8732’, ‘other’: ‘content’},  
{‘policyid’: ‘1012’, ‘other’: ‘content’}

]

I have some code that works locally on my machine:

- name: sort policy  
set\_fact:  
desired\_policy: |  
[  
{% for policy in fwpolicy.meta.results | sort(attribute=‘policyid’) %}  
{  
‘rule’: ‘{{ policy.policyid }}’,  
‘after’: ‘{{ (loop.previtem | default({}) )[‘policyid’] | default(0) }}’,  
‘before’: ‘{{ (loop.nextitem | default({}) )[‘policyid’] | default(0) }}’  
},  
{% endfor %}  
]

When I run this on my machine, I get the following list:

[  
{‘policyid’: ‘1000’, ‘after’: 0, ‘before’: 1009},

{‘policyid’: ‘1009’ , ‘after’: 1000, ‘before’: 1012},

{‘policyid’: ‘1012’ , ‘after’: 1009, ‘before’: 1024},

{‘policyid’: ‘1024’ , ‘after’: 1012, ‘before’: 2019},  
{‘policyid’: ‘2019’ , ‘after’: 1024, ‘before’: 8732},  
{‘policyid’: ‘8732’ , ‘after’: 2019, ‘before’: 0}  
]

On my colleague’s machine (and on AWX for that matter), I get:

[  
{‘policyid’: ‘1000’, ‘after’: 0, ‘before’: 0},

{‘policyid’: ‘1009’ , ‘after’: 0, ‘before’: 0},

{‘policyid’: ‘1012’ , ‘after’: 0, ‘before’: 0},

{‘policyid’: ‘1024’ , ‘after’: 0, ‘before’: 0},  
{‘policyid’: ‘2019’ , ‘after’: 0, ‘before’: 0},  
{‘policyid’: ‘8732’ , ‘after’: 0, ‘before’: 0}  
]

Am I doing something wrong, and should I be doing something differently?
