Create list from multiple HubDB columns and set an order

When I want to display a set of rows, set in a specific order from HubDB, I would code it to look at a specific column, for example:

{% for row in houses | sort(False, False, "house_number") %}

However, is it possible to use multiple HubDB columns and order them on a webpage?

For example in HubDB:
column 1 = 140, column 2 = 22 and column 3 = 110

…the webpage should display:

“22”, (col 2)

“110”, (col 3)

“140” (col 1)

I assume I have to set a list based on all 3 columns and order them accordingly, but I’ve struggled to code it.

Hi @ben-duchy
If I’m understanding correctly I think your best bet is to add those rows to an array and then sort that.
Here’s a quick demo I did with a Pets table.

{% set table = hubdb_table_rows(module.hubdbtable_field) %}

{% for row in table %}
{% set row_array = []%}
{{ row.name }}<br>
{% if row.weight %}
{% do row_array.append( row.weight ) %}
{% endif %}

{% if row.height %}
{% do row_array.append( row.height ) %}
{% endif %}

{% if row.age %}
{% do row_array.append( row.age ) %}
{% endif %}

{% for item in row_array|sort %}
{{ item }}<br>
{% endfor %}

<hr>
{% endfor %}

That’s then outputting smallest number first always regardless of which column it is.


Barry Grennan

Freelance HubSpot CMS Developer

Website | Contact | LinkedIn

Thanks @BarryGrennan,

I’ve got it working, although it errors on the sort function which is strange as it displays correcty in the module preview

@ben-duchy I was getting an error too because most of the rows I hadn’t set the weight, height and age for.
Wrapping the append in the if statement is what solved it for me.


Barry Grennan

Freelance HubSpot CMS Developer

Website | Contact | LinkedIn

That makes sense, although a bit different for me, it seems to error if there’s a decimal place. The columns are set as number field so not sure how I can fix this. I’m confused why the module works in preview but it won’t let me publish it. Thanks for your help thus far anyway :slightly_smiling_face: