# Find highest number in loop

**URL:** <https://community.hubspot.com/t/find-highest-number-in-loop/138834>\
**Category:** CMS Development\
**Tags:** hubdb\
**Created:** [September 1, 2025, 1:35pm UTC](https://community.hubspot.com/t/find-highest-number-in-loop/138834 "2025-09-01T13:35:12Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ben-duchy](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ben-duchy/32/84083_2.png) [@ben-duchy](https://community.hubspot.com/u/ben-duchy)\
**Post date:** [September 1, 2025, 1:35pm UTC](https://community.hubspot.com/t/find-highest-number-in-loop/138834/1 "2025-09-01T13:35:12Z")

</div>

Does anyone know if / how it’s possible to find the highest and lowest number in a HubDB loop? I have a list of values displayed inside a table and I require the number to change colour via CSS if it’s the highest / lowest.

---

<div class="post-metadata">

**Author:** ![BarryGrennan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/barrygrennan/32/97794_2.png) [@BarryGrennan](https://community.hubspot.com/u/BarryGrennan)\
**Post date:** [September 1, 2025, 2:09pm UTC](https://community.hubspot.com/t/find-highest-number-in-loop/138834/2 "2025-09-01T14:09:38Z")

</div>

Hi @ben-duchy,  
I used my doggie HubDB demo from before for this

```lia-code-sample
{% set table = hubdb_table_rows(module.hubdbtable_field) %}

{% set weight_array = [] %}
{% for row in table %}
{% do weight_array.append( row.weight) %}
{% endfor %}
{% set sorted_array = weight_array|sort %}

{% for row in table %}
{{ row.name }}<br>
{{ row.weight }} {% if row.weight == sorted_array[0] %}is lowest{% elif row.weight == sorted_array[-1] %}is highest{% endif %}

<hr>
{% endfor %}

```

The sorted\_array sorts the numbers lowest to highest, then you test your displayed values against sorted\_array[0] (lowest) and sorted\_array[-1] (highest)

I’m sure there’s other ways of doing it.

* * *

 ![profile2022a.jpg](https://d2qpr2jzcazej7.cloudfront.net/original/2X/7/7273a34ccfe3489fceebc8a36eca751481b59531.jpeg)

**Barry Grennan**

[Freelance HubSpot CMS Developer](https://www.seoanseo.ca?utm_source=HubSpotCommunity)

[Website](https://www.seoanseo.ca?utm_source=HubSpotCommunity) | [Contact](https://www.seoanseo.ca/contact?utm_source=HubSpotCommunity) | [LinkedIn](https://www.linkedin.com/in/barrygrennan)

---

<div class="post-metadata">

**Author:** ![GiantFocal](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/giantfocal/32/41969_2.png) [@GiantFocal](https://community.hubspot.com/u/GiantFocal)\
**Post date:** [September 3, 2025, 4:10am UTC](https://community.hubspot.com/t/find-highest-number-in-loop/138834/3 "2025-09-03T04:10:15Z")

</div>

Hi @ben-duchy,

I don’t quite understand the exact implementation plan, but you can do the following if it’s just a matter of finding the highest/lowest numbers in a loop.

Create two variables before the loop: highest and lowest

In your loop, always compare the current row’s data against the highest and lowest variables. If the value is higher/lower, then update the variable with this row’s data.

Once the loop ends, you’ll have two usable variables to target the exact hubDB rows.

---

<div class="post-metadata">

**Author:** ![ben-duchy](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/ben-duchy/32/84083_2.png) [@ben-duchy](https://community.hubspot.com/u/ben-duchy)\
**Post date:** [September 3, 2025, 1:51pm UTC](https://community.hubspot.com/t/find-highest-number-in-loop/138834/4 "2025-09-03T13:51:09Z")

</div>

Thanks @GiantFocal, sounds like you’re on the same page as @BarryGrennan.

Might be a bit more logic for my particular use case, but it gives me a good starting point.
