HubDB: Filter by state alphabetically

Hello there! I want to be able to have my states listed Alphabetically. Since there is no drag and drop feature on HubDB table, I would have to filter and code it out myself…
Here is my filter code:

{% set queryparam = "" %}
{% if request.query_dict.state in ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50"] and request.query_dict.name == "" %}
 {% set queryparam = queryparam ~ "&state=__contains"~request.query_dict.state|urlencode %}

{% endif %}

{% if request.query_dict.state in ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50"] and request.query_dict.name != "" %}
 {% set queryparam = queryparam~"&state="~request.query_dict.state|urlencode~"&zip__icontains="~request.query_dict.zip|urlencode %}
{% endif %}
{% if request.query_dict.state == "show-all" and request.query_dict.name != "" %}
{% endif %}

Here is the code to render data:

<!-- Check if table exists, set sort by state -->
{% if table == [] %}
 <p class='align-center'>Sorry, no listings found for that search. Try changing your filter and search again.</p>
{% else %}
 {% for row in table|sort(False, False, 'state') %}

<div class="consultant-listing">
 <h4>{{ row["name"] }}</h4>

 <div class="information">

{% for state in row.state %}
<div class="fac_state">{{state.name }}
</div>
{% endfor %} 

 <div class="fac_city"">{{ row["city"] }} , {{ row["zip"] }}</div> 
 <div class='location_details'> Locations: {{row["location"]}}</div> 
 <div><i class="fas fa-phone"></i> {{ row["phone"] }}</div>
<br><div class="links"> <i class="fas fa-link"></i> <a href="{{row["website"]}}">Website</a> 
 <br><i class="fas fa-envelope"></i> <a href="mailto:{{ row["email"] }}">{{ row["email"] }}</a>
 </div> 
 {#<div class="location_details">{{ row["location_details"] }}</div>#} 

 </div>
 </div>
 {% endfor %}
 {% endif %}</div>

This is the page. I want the states to be in alphabetical order:

Thank you so much :slightly_smiling_face:

Hey @unicorndev

No, it is not possible here as Sorting is generally available in beta version for more details look at this below link-

https://community.hubspot.com/t5/HubSpot-Ideas/HubDB-rearrange-order/idi-p/5167

Hope this helps!

If we were able to answer your query, kindly help the community by marking it as a solution.

Thanks and Regards.

Yeah, I saw that which is unfortunate. So there is no code to sort this alphabetically?

Hi @unicorndev,

first of all - it is possible to sort your results alphabetically without the beta.

The magic can be done with the orderBy parameter. You can find the available parameters in the API docs which the HubL filter query employs.

I created a Hubdb table that includes a column with a name state which I used for my ordering.

{% set states = hubdb_table_rows(5412942, "orderBy=state" ) %}
<ul>
{% for state in states %}
 <li>
 {{state.state}} 
 </li>
{% endfor %}
</ul>

screengrab of the output and the table

hope that helps

best,

Anton

Thank you, I believe I’m getting closer to the answer. :slightly_smiling_face:
When I added in the code, it turned out like this:

How do I extract the states that way? I tried {{row.state}} but it didn’t work