# Sorting Custom Objects

**URL:** <https://community.hubspot.com/t/sorting-custom-objects/61592>\
**Category:** CMS Development\
**Tags:** enterprise\
**Created:** [July 24, 2022, 1:40am UTC](https://community.hubspot.com/t/sorting-custom-objects/61592 "2022-07-24T01:40:31Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![SStrachan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sstrachan/32/99678_2.png) [@SStrachan](https://community.hubspot.com/u/SStrachan)\
**Post date:** [July 24, 2022, 1:40am UTC](https://community.hubspot.com/t/sorting-custom-objects/61592/1 "2022-07-24T01:40:31Z")

</div>

I am able to return my custom objects with the following, but how can I sort them by loamvalue property (this is a number value I want to sort from high to low)

```lia-code-sample
 {% set yacht = crm_objects("yacht", yachtid, "name,builder,yearbuilt,loamvalue") %}

{% for yacht in yacht.results %}
....
{% endfor %}

```

---

<div class="post-metadata">

**Author:** ![Teun](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/teun/32/28739_2.png) [@Teun](https://community.hubspot.com/u/Teun)\
**Post date:** [July 25, 2022, 6:17am UTC](https://community.hubspot.com/t/sorting-custom-objects/61592/2 "2022-07-25T06:17:44Z")

</div>

Hi @SStrachan ,

This is what the [docs](https://developers.hubspot.com/docs/cms/hubl/functions#crm-objects) describe:

_Results can be sorted using at least one order parameter in the query. For example, crm\_objects(“contact”, “firstname=Bob&order=lastname&order=createdate”) will order contacts with the first name “Bob” by last name and then by createdate. To reverse a sort, prepend - to the property name like order=-createdate._

So in your case, it should be something like this:

` {% set yachts = crm_objects("yacht", "order=loamvalue", "name,builder,yearbuilt,loamvalue") %}`

---

<div class="post-metadata">

**Author:** ![SStrachan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sstrachan/32/99678_2.png) [@SStrachan](https://community.hubspot.com/u/SStrachan)\
**Post date:** [July 25, 2022, 2:19pm UTC](https://community.hubspot.com/t/sorting-custom-objects/61592/3 "2022-07-25T14:19:01Z")

</div>

Hi @Teun

How can I append this with the ID’s I am passing in the second parameter?

---

<div class="post-metadata">

**Author:** ![Teun](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/teun/32/28739_2.png) [@Teun](https://community.hubspot.com/u/Teun)\
**Post date:** [July 25, 2022, 5:43pm UTC](https://community.hubspot.com/t/sorting-custom-objects/61592/4 "2022-07-25T17:43:26Z")

</div>

Hi @SStrachan ,

Can you share the output of ‘yachtid’ from your code? The second param of the crm\_objects function expects a query, so you can pass multiple ID’s in that query. Should look something like this:

“id=\<id\>&id=\<id\>&order=loamvalue”

---

<div class="post-metadata">

**Author:** ![SStrachan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sstrachan/32/99678_2.png) [@SStrachan](https://community.hubspot.com/u/SStrachan)\
**Post date:** [July 26, 2022, 11:41am UTC](https://community.hubspot.com/t/sorting-custom-objects/61592/5 "2022-07-26T11:41:36Z")

</div>

@Teun the yachtid variable returns ID’s like this: [1895666270, 1938411071, 1895119710]

---

<div class="post-metadata">

**Author:** ![Teun](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/teun/32/28739_2.png) [@Teun](https://community.hubspot.com/u/Teun)\
**Post date:** [July 26, 2022, 12:20pm UTC](https://community.hubspot.com/t/sorting-custom-objects/61592/6 "2022-07-26T12:20:46Z")

</div>

Hi @SStrachan ,

Could you try this:

```lia-code-sample
{% set query = 'order=loamvalue' %}

{% for id in yachtid %}
 {% set query = query + "&id=" + id %}
{% endfor %}

{% set yachts = crm_objects("yacht", query, "name,builder,yearbuilt,loamvalue") %}

```

---

<div class="post-metadata">

**Author:** ![SStrachan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sstrachan/32/99678_2.png) [@SStrachan](https://community.hubspot.com/u/SStrachan)\
**Post date:** [July 26, 2022, 12:52pm UTC](https://community.hubspot.com/t/sorting-custom-objects/61592/7 "2022-07-26T12:52:20Z")

</div>

Thanks @Teun That doesnt seem to work.

---

<div class="post-metadata">

**Author:** ![Teun](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/teun/32/28739_2.png) [@Teun](https://community.hubspot.com/u/Teun)\
**Post date:** [July 26, 2022, 12:54pm UTC](https://community.hubspot.com/t/sorting-custom-objects/61592/8 "2022-07-26T12:54:32Z")

</div>

Hi @SStrachan ,

Didn’t have the time to test it.

What is the output of {{query|pprint}}?

```lia-code-sample
{% set query = 'order=loamvalue' %}

{% for id in yachtid %}
 {% set query = query + "&id=" + id %}
{% endfor %}

{{query|pprint}}

{% set yachts = crm_objects("yacht", query, "name,builder,yearbuilt,loamvalue") %}

```

---

<div class="post-metadata">

**Author:** ![SStrachan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sstrachan/32/99678_2.png) [@SStrachan](https://community.hubspot.com/u/SStrachan)\
**Post date:** [July 26, 2022, 12:58pm UTC](https://community.hubspot.com/t/sorting-custom-objects/61592/9 "2022-07-26T12:58:18Z")

</div>

(String: order=loamvalue)

---

<div class="post-metadata">

**Author:** ![SStrachan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sstrachan/32/99678_2.png) [@SStrachan](https://community.hubspot.com/u/SStrachan)\
**Post date:** [July 26, 2022, 12:56pm UTC](https://community.hubspot.com/t/sorting-custom-objects/61592/10 "2022-07-26T12:56:04Z")

</div>

From what I understand, when you pass multiple ID’s they have to be like this: [1895666270, 1938411071, 1895119710]  
So it would look like this:

`{% set yacht = crm_objects("yacht", [1895666270, 1938411071, 1895119710], "name,builder,yearbuilt,loamvalue") %}`

---

<div class="post-metadata">

**Author:** ![SStrachan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sstrachan/32/99678_2.png) [@SStrachan](https://community.hubspot.com/u/SStrachan)\
**Post date:** [July 27, 2022, 10:45am UTC](https://community.hubspot.com/t/sorting-custom-objects/61592/11 "2022-07-27T10:45:31Z")

</div>

@Teun So how can I do this with only the ID’s of the results I want returned?

---

<div class="post-metadata">

**Author:** ![SStrachan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sstrachan/32/99678_2.png) [@SStrachan](https://community.hubspot.com/u/SStrachan)\
**Post date:** [July 26, 2022, 1:53pm UTC](https://community.hubspot.com/t/sorting-custom-objects/61592/12 "2022-07-26T13:53:28Z")

</div>

In the documentation it shows 2 examples:

```lia-code-sample
{# Render custom objects by query #}
{% set events = crm_objects("event", "limit=3&type=virtual") %}
<h3>{{events.total}} New Events:<h3>
<ul>
{% for event in events.results %}
	<li>Name: {{ event.name }}</li>
{% endfor %}
<ul> 

{# Render custom objects by ids #}
{% set events = crm_objects("event", [289236,289237,289238]) %}
<h3>{{events.total}} New Events:<h3>
<ul>
{% for event in events.results %}
	<li>Name: {{ event.name }}</li>
{% endfor %}
<ul>

```

How would you sort multiple ID’s?

---

<div class="post-metadata">

**Author:** ![Teun](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/teun/32/28739_2.png) [@Teun](https://community.hubspot.com/u/Teun)\
**Post date:** [July 27, 2022, 6:45am UTC](https://community.hubspot.com/t/sorting-custom-objects/61592/13 "2022-07-27T06:45:05Z")

</div>

Hi @SStrachan ,

I found the example you shared above, using an array does not allow us to pass the ‘order’ param.

Could you try to use the [|sort](https://developers.hubspot.com/docs/cms/hubl/filters#sort) filter instead?

Should look something like this:

```lia-code-sample
{% for yacht in yacht.results|sort(false, false, "loamvalue") %}
....
{% endfor %}

```

---

<div class="post-metadata">

**Author:** ![SStrachan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/sstrachan/32/99678_2.png) [@SStrachan](https://community.hubspot.com/u/SStrachan)\
**Post date:** [July 27, 2022, 12:03pm UTC](https://community.hubspot.com/t/sorting-custom-objects/61592/14 "2022-07-27T12:03:52Z")

</div>

How does hubspot determine the order of an array when returned? When I rearrange the order of the array, it doesn’t seem to change the output.

---

<div class="post-metadata">

**Author:** ![ABarna](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@ABarna](https://community.hubspot.com/u/ABarna)\
**Post date:** [October 11, 2023, 1:11am UTC](https://community.hubspot.com/t/sorting-custom-objects/61592/15 "2023-10-11T01:11:43Z")

</div>

Hi @Teun  
Would you be able to help here(please)?  
I have a developer who is using custom objects and we are trying to sort by one of the properties which has 4 possibly options.  
The property is Occupancy Status and the options are either Available, Available Soon, Viewing & Occupied.  
We would like to have the results sorted based on the above. However we have used the suggested code and the result is showing alphabetical order - Available, Available Soon, Occupied & Viewing. Are you able to suggest a way to override this?  
Much appreciated
