# String collation

In BBC BASIC, strings are well-ordered and can be compared to one another in the same way as numbers, using `=`, `<`, `>`, `<=`, `>=` and `<>`. The comparison operators have been designed to lead to a collation sequence similar to those used for telephone directories and library catalogues.

The equality of strings is straightforward: two strings are equal if and only if they are both the empty string, `""`, or they have the same length and the characters at each position are equal in both strings. Note that BASIC strings may contain characters of 'ASCII' value 0 to 255.

`=` returns `TRUE` iff its two operands are equal, `FALSE` otherwise. `<>` returns `FALSE` iff they are equal, `TRUE` otherwise.

The comparison of strings is more complicated, but only slightly. Let `A\$` and `B\$` be strings, and let us start at the first character of each.

• If we are beyond the end of both strings, then `A\$` is equal to `B\$`.
• Otherwise if we are beyond the end of `B\$`, then `A\$` is greater than `B\$`.
• Otherwise if we are beyond the end of `A\$`, then `A\$` is less than `B\$`.
• Otherwise if the current character in `A\$` has a higher 'ASCII' value than the one in `B\$`, then `A\$` is greater than `B\$`.
• Otherwise if the current character in `A\$` has a lower 'ASCII' value than the one in `B\$`, then `A\$` is less than `B\$`.
• Otherwise the current characters are both present and equal, and we advance to the next character in both strings, and start again.

Therefore the longer string is not necessarily the greater one. For example, `"Sandra"` is greater than `"Sand"`, but less than `"Sandy"`. The empty string, `""`, is less than any other string. Note that the collation sequence is case-sensitive, and with regard to ASCII, any uppercase letter compares less than any lowercase one.

`<` returns `TRUE` iff the left-hand operand is less than the right, `FALSE` otherwise. `>` returns `TRUE` iff the left-hand operand is greater than the right, `FALSE` otherwise.

`<=` returns `TRUE` iff the left-hand operand is less than, or equal to, the right, `FALSE` otherwise. `>=` returns `TRUE` iff the left-hand operand is greater than, or equal to, the right, `FALSE` otherwise.

-- beardo 23:07, 24 September 2007 (BST)