STRING_COMPARE

The function STRING_COMPARE compares two string and outputs a Number indicating their lexicographical order.

This function takes two strings as input. It outputs a Number that indicates the lexicographical order of the input strings. If the first string comes before the second in lexicographical order, output is -1. If the second string comes before the first, output is 1. If the strings are identical, output is 0.

Declaration

STRING_COMPARE(string1, string2) -> comparison_result

Parameters

string1 (required, type: string)
The first string.

string2 (required, type: string)
The second string.

Return Values

comparison_result (type: Number)
The number that indicates the lexicographical order of string1 and string2. -1 indicates that the first string comes before the second, 1 means that the second string comes before the first, and 0 means that the strings are identical.

Examples

The following example compares the string "Apples" with the string "Bananas". "A" comes before "B" alphabetically, so STRING_COMPARE indicates that the first string, "Apples", comes before the second string, "Bananas", in lexicographical order:

STRING_COMPARE("Apples", "Bananas") -> -1

The following example again compares the string "Apples" with the string "Bananas", but this time "Apples" is listed after "Bananas", so STRING_COMPARE indicates that the first string, "Bananas", comes after the second string, "Apples", in lexicographical order:

STRING_COMPARE("Bananas", "Apples") -> 1

The following example compares the string "Apples" with the identical string "Apples", and STRING_COMPARE indicates that they are, in fact, identical:

STRING_COMPARE("Apples", "Apples") -> 0

The variant of lexicographical ordering used by STRING_COMPARE places short strings before longer ones. The following example compares the string "Apples" with the string "Apples and more" and indicates that "Apples" comes first:

STRING_COMPARE("Apples", "Apples and more") -> -1

Lexicographical ordering is case sensitive; in the variant used by STRING_COMPARE, lowercase letters come before uppercase. The following example compares the string "Apples" with the string "apples" and indicates that "apples" comes first:

STRING_COMPARE("Apples", "apples") -> 1

Strings can contain numbers as well as letters. The variant of lexicographical ordering used by STRING_COMPARE places numbers before letters. The following example compares the string "A" with the string "9" and indicates that "9" comes first:

STRING_COMPARE("A", "9") -> 1

Discussion

"Lexicographical" is derived from the word "lexicon," meaning the set of characters used in a language. The characters in a lexicon have a conventional ordering, such as the letters in the English alphabet. Putting words in alphabetical order is putting them in lexicographical order.

While the letters in the English alphabet have a well-established order, their place in relation to non-letter characters is less standardized. There exist variants of lexicographical ordering that arrange some non-letter characters differently. The variant used by STRING_COMPARE (and, in fact, all of Airscript) places lowercase letters before capital, symbols and punctuation before numbers, numbers before letters, and empty spaces before occupied spaces (that is, shorter strings before longer strings).