# Sorting a list using an optional attribute

**URL:** <https://community.hubspot.com/t/sorting-a-list-using-an-optional-attribute/39134>\
**Category:** CMS Development\
**Created:** [June 17, 2021, 10:29pm UTC](https://community.hubspot.com/t/sorting-a-list-using-an-optional-attribute/39134 "2021-06-17T22:29:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jakwevagbe](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jakwevagbe/32/2401_2.png) [@jakwevagbe](https://community.hubspot.com/u/jakwevagbe)\
**Post date:** [June 17, 2021, 10:29pm UTC](https://community.hubspot.com/t/sorting-a-list-using-an-optional-attribute/39134/1 "2021-06-17T22:29:38Z")

</div>

Is it possible to sort on an attribute that is optional? For example, If I have a list like this,

`{% set list = [{ "name": "John", "id": 1}, { "name": "", "id": 2}] %}`

Is it possible to sort the list above by name? Using |sort(False, False, ‘name’) doesn’t seem to work. How do I achieve sorting the list above?

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [June 18, 2021, 4:04pm UTC](https://community.hubspot.com/t/sorting-a-list-using-an-optional-attribute/39134/2 "2021-06-18T16:04:59Z")

</div>

@jakwevagbe

Are you looping through them?  
I tested and it seems to sort as expected. (the name that is null is returned first in order)

My test code:

```lia-code-sample
{% set list = [{ "name": "John", "id": 1}, { "name": "", "id": 2}, { "name": "will", "id": 8}, { "name": "ared", "id": 9}] %}

{% for x in list|sort(False, False, "name") %}
{{ loop.index }} + {{ x.name }} + {{ x.id }}<br>
{% endfor %}

```

---

<div class="post-metadata">

**Author:** ![jakwevagbe](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jakwevagbe/32/2401_2.png) [@jakwevagbe](https://community.hubspot.com/u/jakwevagbe)\
**Post date:** [June 29, 2021, 5:16pm UTC](https://community.hubspot.com/t/sorting-a-list-using-an-optional-attribute/39134/3 "2021-06-29T17:16:56Z")

</div>

It doesn’t work for me. It returns empty. The list is the result of a HubDb query and is actually in the form of

`[{id=38678890961, name=''}, {id=38678890962, name='Jane'}]`

---

<div class="post-metadata">

**Author:** ![dennisedson](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/dennisedson/32/28044_2.png) [@dennisedson](https://community.hubspot.com/u/dennisedson)\
**Post date:** [June 30, 2021, 3:24pm UTC](https://community.hubspot.com/t/sorting-a-list-using-an-optional-attribute/39134/4 "2021-06-30T15:24:30Z")

</div>

@alyssamwilie ,

Any additional thoughts here?

@jakwevagbe , could you drop in the code you are using?
