How String Comparison happens in Swift

Swift strings are compared according to the
Unicode Collation Algorithm,
which means that (effectively),

In your example, "hello” and "Hello" have the Unicode values

hello: U+0068, U+0065, U+006C, U+006C, U+006F 
Hello: U+0048, U+0065, U+006C, U+006C, U+006F 

and therefore "Hello" < "hello".

The “normalization” or “decomposing” is relevant e.g. for characters
with diacritical marks. As an example,

a = U+0061
รค = U+00E4
b = U+0062

have the decomposed form

a: U+0061
รค: U+0061, U+0308  // LATIN SMALL LETTER A + COMBINING DIAERESIS
b: U+0062

and therefore "a" < "รค" < "b".

For more details and examples, see What does it mean that string and character comparisons in Swift are not locale-sensitive?

Leave a Comment