14. The combined comparison operator


#1

I was wondering if anyone could help me understand this better. I understand how to do the command and i passed the exercise with no problems, but why is book1 greater than book 2? Is it because of alpha characters? length of characters? because it comes first? What exactly makes it greater (give the output of positive 1)? Thanks in advance.


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


book_1 = "A Wrinkle in Time"

book_2 = "A Brief History of Time"

book_1 <=> book_2


#2

It is the ordinals that determine before or after.

'ab' comes before 'aw`.

If we add up the ordinals (the character codes).

97 + 98  == 195
97 + 119 == 216

#3

See and thats what I thought, but then I got confused because I changed the text to "A a" and left the "A Brief History of Time" the same, and I still got positive 1. Shouldn't that of been -1, since a would come before b? However, if i change it to, "A A", then I get a -1 instead of a 1. Sorry just trying to understand better.


#4

The ordinal for B is smaller than that of b, so aB will come before ab.

If we want to ignore case, the we need to convert all text to one case or the other when comparing.


#5

Gotcha. Thankyou for helping me understand!


#6

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