Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7090861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:06:32+00:00 2026-05-28T08:06:32+00:00

I’m going through the tutorial Bastards Book of Ruby and I’m having trouble understanding

  • 0

I’m going through the tutorial Bastards Book of Ruby and I’m having trouble understanding how the function sort_by works. Here’s the exercise:

Exercise: Sort names by last name

Given an array of strings that represent names in “FIRSTNAME LASTNAME” form, use sort_by and split to return an array sorted by last names. For simplicity’s sake, assume that each name consists of only two words separated by space (i.e. only “John Doe” and not “Mary Jo Doe”).

names = ["John Smith", "Dan Boone", "Jennifer Jane", "Charles Lindy", "Jennifer Eight", "Rob Roy"]

And here’s the solution:

names = ["John Smith", "Dan Boone", "Jennifer Jane", "Charles Lindy", "Jennifer Eight", "Rob Roy"]
sorted_names = names.sort_by do |name|
   name.split(" ").reverse.join.upcase
end

puts sorted_names.join('; ')
# output=> Dan Boone; Jennifer Eight; Jennifer Jane; Charles Lindy; Rob Roy; John Smith

However, when I run the code

sorted_names = names.sort_by do |name|
  puts name.split(" ").reverse.join.upcase
end

I get the output:

SMITHJOHN
BOONEDAN
JANEJENNIFER
LINDYCHARLES
EIGHTJENNIFER
ROYROB

which is quite different than the output from puts sorted_names.join('; ')

I thought the method was actually manipulating the data (hence name.split(" ").reverse.join.upcase) and then saving it to the new array sorted_names. But obviously it’s not. So, my question is can someone please explain why this sort_by method is acting this way. I’m relatively new to Ruby and I tried looking through the Ruby docs for an explanation but couldn’t find one. I feel I’m not understanding an important concept in Ruby and would appreciate any help or insight.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:06:33+00:00Added an answer on May 28, 2026 at 8:06 am

    sort_by is kind of like doing this:

    a.map {|item| [f(item), item]}.sort.map {|key, item| item}
    

    That is, the sort_by block is used to calculate a sort-key for each value in the array, and then those keys are sorted, and then those keys are mapped back to the original values.

    So what you’re seeing are those keys, before they’re used to sort. Except as ranksrejoined noted, puts returns nil, so by examining the keys you’ve broken the function!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I am reading a book about Javascript and jQuery and using one of the
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.