14. The Combined Comparison Operator <=> understanding the logic


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-ET4bU/3/3?curriculum_id=5059f8619189a5000201fbcb


I got the code to pass through, but what I find difficult to comprehend is understanding why "book1" is GREATER than "book2"?

A Wrinkle in Time vs. A Brief History of Time

I understand it looks at the first character of the string, compares it, then moves to the next character. Having said that, shouldn't book2 be greater because "B" comes before "W"?

Thanks!


book_1 = "A Wrinkle in Time"

book_2 = "A Brief History of Time"

book_1 <=> book_2

1


#2

Less than means, before and greater than means, after.

 > "a" < "b"
=> true
 > "b" > "a"
=> true

Which is correct. Wrinkle does follow Brief. In which case there is a swap.


#3

gotcha! That was confusing for awhile, thanks for clarifying :slight_smile:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.