# Trouble sorting DB queries by date

**URL:** <https://community.hubspot.com/t/trouble-sorting-db-queries-by-date/3540>\
**Category:** CMS Development\
**Created:** [October 10, 2017, 3:05pm UTC](https://community.hubspot.com/t/trouble-sorting-db-queries-by-date/3540 "2017-10-10T15:05:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jleibham](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jleibham/32/52876_2.png) [@Jleibham](https://community.hubspot.com/u/Jleibham)\
**Post date:** [October 10, 2017, 3:05pm UTC](https://community.hubspot.com/t/trouble-sorting-db-queries-by-date/3540/1 "2017-10-10T15:05:52Z")

</div>

"Hi there,

I have a DB setup with all my companies events and query them onto my event page. I recently wanted to add some functionality that if I was to add another DB entry for an event before the last DB entry it would appear in the right spot on the event page. I want to use a filter to sort my DB entries by date before they are queried to the page. I found this piece of code to use:

```auto
{% for row in hubdb_table_rows(<tableId>, <filterQuery>) %}
 the value for row {{ row.hs_id }} is {{ row.<column name> }}
{% endfor %}

```

I thought the filterQuery “orderBy=-bar” seems like it could be used to sort by date. But my DB query setup is a little different from what has been displayed above and I’m not sure how to get this to work on my own page.

I currently use:

`{% set table = hubdb_table_rows(673755, queryparam)%}`

and I tried to implement the code this way:

`{% set table = hubdb_table_rows(673755, queryparam, orderBy=event_date)%}`

I also tried a few other ways but can’t seem to patch this together. Can someone explain what I’m doing wrong?  
Thank you,

Jon

---

<div class="post-metadata">

**Author:** ![anthonypizzurro](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/anthonypizzurro/32/44503_2.png) [@anthonypizzurro](https://community.hubspot.com/u/anthonypizzurro)\
**Post date:** [October 13, 2017, 1:32pm UTC](https://community.hubspot.com/t/trouble-sorting-db-queries-by-date/3540/2 "2017-10-13T13:32:32Z")

</div>

Hi Jon,

The `hubdb_table_rows()` function only accepts 2 parameters, so adding a 3rd won’t do anything. What you’ll need to do is append `&orderBy=event_date` to `queryparam`, wherever that is being defined.

Instead of:

`{% set table = hubdb_table_rows(673755, queryparam, orderBy=event_date)%}`

You’d want:

```auto
{% set queryparam = "orderBy=event_date" %}

{% set table = hubdb_table_rows(673755, queryparam)%}

```

I think that should do the trick!

Best,

Anthony
