Sorting Custom Objects

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)

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

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

Hi @SStrachan ,

This is what the docs 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") %}

Hi @Teun

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

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”

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

Hi @SStrachan ,

Could you try this:

{% set query = 'order=loamvalue' %}

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

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

Thanks @Teun That doesnt seem to work.

Hi @SStrachan ,

Didn’t have the time to test it.

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

{% 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") %}

(String: order=loamvalue)

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") %}

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

In the documentation it shows 2 examples:

{# 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?

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 filter instead?

Should look something like this:

{% for yacht in yacht.results|sort(false, false, "loamvalue") %}
....
{% endfor %}

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.

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