user1840378
user1840378

Reputation: 349

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?

Upvotes: 2

Views: 94

Answers (1)

Thilo
Thilo

Reputation: 262684

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.

Upvotes: 3

Related Questions