# HubDB Sorted By Certain Column

**URL:** <https://community.hubspot.com/t/hubdb-sorted-by-certain-column/43744>\
**Category:** CMS Development\
**Tags:** hubdb\
**Created:** [September 13, 2021, 6:55pm UTC](https://community.hubspot.com/t/hubdb-sorted-by-certain-column/43744 "2021-09-13T18:55:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![cives](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/cives/32/57633_2.png) [@cives](https://community.hubspot.com/u/cives)\
**Post date:** [September 13, 2021, 6:55pm UTC](https://community.hubspot.com/t/hubdb-sorted-by-certain-column/43744/1 "2021-09-13T18:55:12Z")

</div>

Trying to use HubDB to create an attendee list as such:  
**Company Name 1**  
Employee 1  
Employee 2  
**Company Name 2**  
Employee 1  
Employee 2  
Is there an example/use case that goes over this sort of filtering? Right now I have a simple table that includes every attendee and their company, but I think the above is cleaner. My table has two columns, name, and company.

---

<div class="post-metadata">

**Author:** ![miljkovicmisa](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/miljkovicmisa/32/86357_2.png) [@miljkovicmisa](https://community.hubspot.com/u/miljkovicmisa)\
**Post date:** [September 13, 2021, 8:53pm UTC](https://community.hubspot.com/t/hubdb-sorted-by-certain-column/43744/2 "2021-09-13T20:53:16Z")

</div>

Hello @cives !  
This possible with the following code, I have included further instructions and descriptions inside the comments of the snippet:

```lia-code-sample
<!-- 1. helper variable to store the company to compare with the next one -->
{% set companyName = '' %}

<!-- 2. using the definition list html element as it is more appropriate with the company in the title -->
<dl>
<!-- 3. loop through the rows and order by company [REPLACE <HUBDB TABLE ID> AND <COMPANY ROW NAME> WITH YOUR ACTUAL HUBDB TABLE ID AND COMPANY ROW NAME FROM YOUR TABLE] -->
{% for row in hubdb_table_rows(<HUBDB TABLE ID>, "orderBy=<COMPANY ROW NAME>") %}
<!-- 4. check if the helper variable is empty (first item) or differrent from the previous one (we are in the next company in order) -->
{% if companyName eq '' or companyName ne row.company %}
<!-- 5. output the company name only if the above is true, means that while it is the same company it is not output -->
 <dt>{{ row.company }}</dt>
{% endif %}
<!-- 6. after we output company name we output every employee from that company -->
 <dd>{{ row.name }}</dd>
<!-- 7. now we store the company name so that we can compare it with the next one -->
{% set companyName = row.company %}
{% endfor %}
</dl>

```

See more info about the `hubdb_table_rows` function [in this link](https://developers.hubspot.com/docs/cms/hubl/functions#hubdb-table-rows).

_ **If my answer was helpful please mark it as a solution.** _
