I don’t understand why the output is 1.
Isn’t book_1 less than book_2?
In what way are they being compared?
I would assume the length of the values are what are being considered but that wouldn’t make sense since book_2 is obviously larger than book_1.
Even if it was alphabetical-wise, book_1 would still be less than book_2 because the B in book_2 comes before the letter W in book_1.
No, it is not. Sorting is done by ordinal (simply put, ASCII code). The sorting algorithim is deep and goes to the next characters as long as there are some. In this case,
W comes after
B so book_2 comes up first in the sort order.
You are correct in assessing this as irrelevant. Length has no role in sorting unless that is the parameter for the sort. For that we would need a callback in our sort method (ignore this for now).
Recall that default sort order is ascending. lowest to highest. ‘B’ => 66; ‘W’ => 87.
Ohhhh, now it makes sense! Thanks for the clarification. I did do some research but all that I was able to find was this from 5 years ago: https://www.codecademy.com/en/forum_questions/51c7b7417c82caedf0006f98
It some what cleared it up for me but didn’t emphasis on
““Comes first” alphabetically means “is smaller” in terms of
Which is why I was confused.
Thank you for the explanation, much appreciated!
The beauty is that we can write a method for sort to use when deciding what to do. It most likely isn’t covered in the course to any extent but put a NB on this topic so you continue to study sorting methodology. It will never get old; I can assure you that.
Ruby, like Python is backed up by a C library which no doubt uses some form of Quick Sort. I’m not sure the origin but I believe it was a scientist in 70’s or 80’s.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.