Extracurricular - How would I sort the results of "21.Passing Your Lambda to a Method"


#1



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


My code works but I was just wondering how I would sort the output in alphabetical order. I've been trying .sort and .sort_by in various places after doing a bit of research but I can't get the results to display in alphabetical order. A hint or a tip would be awesome. Thanks.


crew = {
  captain: "Picard",
  first_officer: "Riker",
  lt_cdr: "Data",
  lt: "Worf",
  ensign: "Ro",
  counselor: "Troi",
  chief_engineer: "LaForge",
  doctor: "Crusher"
}

first_half = lambda { |key, value| value < "M" }

a_to_m = crew.select(&first_half)


#2

I think I figured this out. I called the variable a_to_m and used .sort_by on that.

print a_to_m.sort_by { |x, y| y }

If anyone has anything to add, correct or improve please reply!


#3

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