Norbert Girch
Norbert Girch

Reputation: 106

How to compare liquid variables?

I have 5 liquid variables and they all hold a number. For example {{ wa }} outputs 100. Is there a way to compare these values and output the highest number? I've tried this in Liquid to no avail:

{% if wa > bh and wa > ag and wa > jr and wa > jg %}<span>{{ wa }}</span>
  {% elsif bh > wa and bh > ag and bh > jr and bh > jg %}<span>{{ bh }}</span>
  {% elsif ag > wa and ag > bh and ag > jr and ag > jg %}<span>{{ ag }}</span>
  {% elsif jr > wa and jr > bh and jr > ag and jr > jg %}<span>{{ jr }}</span>
  {% elsif jg > wa and jg > bh and jg > ag and jg > jr %}<span>{{ jg }}</span>
{% endif %}

Cheers!

Upvotes: 3

Views: 4611

Answers (1)

drip
drip

Reputation: 12933

You can do something like this:

{% assign numbers = '12,323,9,121,11,1,1116,12,3,1' %}
{% assign numbers_array = numbers | split: ',' %}

{% assign highest_number = numbers_array | first | plus: 0 %}

{% for number in numbers_array %}
    {% assign cur_number = number | plus: 0 %}

    {% if cur_number >= highest_number %}

        {% assign highest_number = number | plus: 0 %}
    {% endif %}
{% endfor %}

The Highest Number is: {{ highest_number }}

Where you create a string with all of the numbers separated with , and split them by , in order to create an array.

You save the first number as the highest and start a loop to compare each number to the current number set in the highest_number. If the cur_number is higher we set that number to highest_number.

That's pretty much the logic.

Upvotes: 3

Related Questions