Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How String Comparison happens in Swift

Tags:

string

swift

in this example:

var str1 = "hello"
var str2 = "Hello"

if str1 < str2 { print("hello is less than Hello")}
else {print("hello is more than Hello")}

on what basis it is found that str1 is greater than str2?

like image 597
KawaiKx Avatar asked Aug 12 '16 04:08

KawaiKx


2 Answers

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

  • each string is put into "Unicode Normalization Form D",
  • the unicode scalar values of these "decomposed" strings are compared lexicographically.

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?

like image 159
Martin R Avatar answered Nov 18 '22 04:11

Martin R


The two strings are compared, character by character, using each character's Unicode value. Since h has a higher code (U+0068) than H (U+0048), str1 is "greater" than str2.

Based on Martin's comment below the question, it's slightly more complex than I stated. Please see What does it mean that string and character comparisons in Swift are not locale-sensitive? for more detail.

like image 20
rmaddy Avatar answered Nov 18 '22 02:11

rmaddy