max() On Strings¶
The min() and max() functions return the minimum or the maximum value of items in an array. This works on integers, but it also works on strings.
In the case of string, the comparison is made with the spaceship operator, so the letters are processed in alphabetical order.
The comparison is case-sensitive: upper-case letter have precedence over lower-case letter. That is usually the expected behavior.
Outside the alphabet range, the order of the strings is based on the ascii number of the characters in the string: then, digits are ranking lower than letters and will always appear first. And then, all even lower-ascii characters, such as %, will appear first. This also means that it is recommended to trim() the strings to avoid getting strings with a leading space first as minium.
It also means that multi-bytes characters are sorted by ascii code of their bytes, not as a whole. Languages, like Chinese, usually do not sort text, so this feature may not be used anyway.
See Also¶
min-max on strings [Try me]