user1840378 user1840378 - 8 months ago 74
Swift Question

Trouble understanding Swift Tuple comparisons

I was reading through Apple's "The Swift Programming Language" (Swift 3 beta) and I'm having a difficult time understanding how comparing two tuples work. The book says

(1, "zebra") < (2, "apple") equates to true.

But shouldn't it be false since "zebra" < "apple" is false? The book also says that

(3, "apple") < (3, "bird") equates to true

But shouldn't this also be false since 3 < 3 is false?


This is a multi-field sort.

First sorting is done by the first entry. Ties are decided by the second entry (and so forth if the tuple is longer).

This is the same process as sorting names in a telephone book (first by family name, ties broken by given name).

In your example, 1 < 2 already and 3 == 3 leads to tie-breaker apple < bird.