Compare
Compares two strings lexicographically and returns -1, 0, or 1 indicating the relationship between them.
Common Properties
- Name - The custom name of the node.
- Color - The custom color of the node.
- Delay Before (sec) - Waits in seconds before executing the node.
- Delay After (sec) - Waits in seconds after executing node.
- Continue On Error - Automation will continue regardless of any error. The default value is false.
info
If the ContinueOnError property is true, no error is caught when the project is executed, even if a Catch node is used.
Inputs
- First String - The first string to compare.
- Second String - The second string to compare.
Options
- Ignore Case - When enabled, performs case-insensitive comparison. Default is false.
Output
- Result - Comparison result as an integer:
-1if First String is lexicographically less than Second String0if both strings are equal1if First String is lexicographically greater than Second String
How It Works
The Compare node performs lexicographical (dictionary order) comparison between two strings:
- Takes two input strings
- Optionally converts both to lowercase if "Ignore Case" is enabled
- Compares character by character using Unicode values
- Returns an integer indicating the relationship
Lexicographical comparison works like dictionary ordering:
- "apple" < "banana" (returns -1)
- "hello" = "hello" (returns 0)
- "zebra" > "apple" (returns 1)
Usage Examples
Example 1: Alphabetical Sorting
Check if names are in alphabetical order:
- First String:
"Smith" - Second String:
"Johnson" - Ignore Case: Enabled
- Result:
1(Smith comes after Johnson)
Example 2: Version Comparison
Compare version strings:
- First String:
"1.0.5" - Second String:
"1.0.10" - Result:
-1(string comparison, not numeric)
warning
For version comparison, use numeric comparison on split components instead of string comparison.
Example 3: Case-Sensitive vs Case-Insensitive
Compare with case sensitivity:
- First String:
"Apple" - Second String:
"apple" - Ignore Case: Disabled
- Result:
-1(uppercase 'A' comes before lowercase 'a' in Unicode)
With case insensitivity:
- First String:
"Apple" - Second String:
"apple" - Ignore Case: Enabled
- Result:
0(equal when ignoring case)
Tips
- Use Ignore Case option for user-facing comparisons where case shouldn't matter
- For numeric comparisons, convert strings to numbers first using ToNumber node
- Combine with If node to create conditional logic based on comparison results
- Lexicographical comparison is byte-by-byte, so "10" < "2" (string order, not numeric)
- Empty strings are considered less than non-empty strings
Common Errors and Solutions
Error: Unexpected comparison results with numbers
- Solution: String comparison is lexicographical, not numeric. Convert to numbers for numeric comparison.
Error: Unicode/special characters producing unexpected results
- Solution: Comparison is based on Unicode values. Ensure strings are normalized.