14. The Combined Comparison Operator


#1

How does the CCO evaluate strings? The tutorial is not clear. The first letters in the example are both "A" with the second letters being "b" and "w" consecutively. The operator makes perfect sense when dealing with numbers. Does it take the binary value of the letter?

Thank you!


#2

I'm having trouble understanding what you mean by this.

Which operator are you talking about? Is it <=>?


#3

The strings are compared alphabetically. This is done (internally by implementation of the <=> operator) by first comparing the first character of the two strings, then if those are equal, comparing the second character of the two strings, and so on. And yes, that is done by comparing the binary values of the characters.

(Note: this is a simplified explanation. With Unicode and other I18N factors, the actual story is considerably more complex.)


#4

Yes I was referring to the <=>.


#5

Can you please help me?...I am stuck in "No man's land"!!!!

Here is my code:

books = [ "A Brief History of Time", "A Wrinkle in Time"]
books.sort! {|book_1, book_2| book_1 <=> book_2}

Here is what I am told when I try to save & submit

"Oops, try again. It looks like your comparison doesn't return the correct value (1). Did you remember to compare book_1 <=> book_2 (in that order)?"

My brain is soup at the moment :frowning: Any advice would be greatly appreciated! Thanks in advance too!


#6

What exercise are you answering? It sounds like they just want the comparison function, not the sort.


#7
  1. The Combined Comparison Operator

#8

sorry 14. The Combined Comparison Operator


#9

Read the exercise more carefully and understand what it asks for. The solution is simply

book_1 = "A Wrinkle in Time"
book_2 = "A Brief History of Time"
book_1 <=> book_2


#10

OMG....thankyou so much....clearly I am misreading the intent of the question (as in I am trying to read other meanings, than what it simply requests! Doh!!!!

Cheers :smile:


#11

The title gives it away: "The Combined Comparison Operator" -- pretty clear that all they want is an expression with that operator. But it was clear to me before you told me what the title was, just from the error message:

"Oops, try again. It looks like your comparison doesn't return the correct value (1). Did you remember to compare book_1 <=> book_2 (in that order)?"

All they are doing is looking at the value returned by your code, and seeing that it isn't 1. That's what the program checking the answers does ... it looks at the results. There's no way that it's going to look inside of

books = [ "A Brief History of Time", "A Wrinkle in Time"]
books.sort! {|book_1, book_2| book_1 <=> book_2}

and see that there's a comparison operator and figure out what value it returns for those books ... think about what that would involve -- the comparison operator is only executed when the block containing it is called somewhere in the bowels of the sort! function.

I point this out with the hope that it will help you interpret other exercises and other error messages that you encounter.


#12

Thankyou again....and I will take your suggestions and try to apply that methodology of thought!

Cheers! :wink:


#13

Great ... that response makes my effort at explaining it worthwhile, so thank you!


#14

books.sort! {|book_1, book_2| book_2 <=> book_1}

You need to reverse the order of book_1 and book_2 so that the ascending order becomes descending.


#15

Hello

I was expecting -1 as the output but it is 1.

After reading your explanation, did the CCO return 1 because the second values from each books are W (book_1) and B (book_2)?

Thank you.


#16

Yeah, codecademy is a great service, but they barely explain this operator. This stackoverflow does a good job abreaking it down: