Answers for "ruby sort array in hash"

1

sort hash ruby

people = {
  :fred => 23,
  :joan => 18,
  :pete => 54
}

# First option
people.values.sort    # => [18, 23, 54]

# Seconf option
people.sort_by { |name, age| age }
  # => [[:joan, 18], [:fred, 23], [:pete, 54]]
Posted by: Guest on March-22-2021

Browse Popular Code Answers by Language