Comparisons in Liquid

How to compare

When comparing variables always make sure you are using the correct data types. If the operand types are mismatched the system tries to make the operation work by converting one of the operands like this:

comparing string with numeric - the system tries to convert the string operand to numeric. If this is not possible both operands will be treated as string. For example:

5 > "7" - "7" will be converted to numeric and the operation will be false

5 > "a132" - "a123" cannot be compared to numeric, this will be treated as "5" > "a123"

comparing string with dateTime -the system will try to convert the string operand to a dateTime variable, if this fails the operands will be compared as string. For example:

2015-01-13T10:47:54 > "2012-01-13T10:47:54" - the second operand will be converted to dateTime and the operation will be true

2015-01-13T10:47:54 > "Jul 2012" - the second operand cannot be compared to dateTime and the comparison will be treated as a string to string comparison.

To convert variables take a look at the convert filter in the Supported filters article.

Number variables

Comparing numeric values is pretty straight forward. You can use any of the operators detailed in the - Supported operators article, for example:

In this example we are comparing a string variable with a numeric one. The string variable however cannot be converted to a string so the comparison will be between two string variables - "524a1" and "3". In this case 524a1 is considered larger.

Date variables

Comparing date type variables is straight forward. Provided at least one of the operands is properly formatted as dateTime and the other operand is either dateTime or string any of the operators detailed in the Supported operators article can be used.