Short answer
h = {:john => 18, :anne => 24, :beth => 35, :paul => 33}
Sort by key
h.sort.to_h # Output: => {:anne=>24, :beth=>35, :john=>18, :paul=>33}
Sort by value
h.sort_by {|_,v| v}.to_h # Output: => {:john=>18, :anne=>24, :paul=>33, :beth=>35}
Both sort and sort_by methods return arrays, hence the need to convert the result back into a hash with to_h.
Long answer (learn more abour sort)
Unlike the Array class, which provides its own implementation of the sort method, the Hash class uses the sort method implemented in the Enumerable module.
Array.instance_method(:sort) # Output: => #<UnboundMethod: Array#sort>
Hash.instance_method(:sort) # Output: => #<UnboundMethod: Hash(Enumerable)#sort>
Objects such as numbers and strings, which can be compared (amongst themselves) in terms of being greater or smaller than others, provide the <=> method, also known as the spaceship method. When comparing two objects, <=> returns -1 if the first object is lesser than the second (a < b), 0 in case they are equal (a == b) and 1 when the first object is greater than the second (a > b).
5 <=> 8 # Output: => -1
5 <=> 5 # Output: => 0
8 <=> 5 # Output: => 1
Most comparable or sortable object classes, such as Integer, Float, Time and String, include a mixin called Comparable, which provides the following comparison operators: < (less than), <= (less than or equal), == (equal), > (greater than), >= (greater than or equal). These methods use the spaceship operator under the hood.
Let's find out which classes include the Comparable mixin:
ObjectSpace.each_object(Class).select { |c| c.included_modules.include? Comparable }
# Output: => [Complex, Rational, Time, File::Stat, Bignum, Float, Fixnum, Integer, Numeric, Symbol, String, Gem::Version, IRB::Notifier::NoMsgNotifier, IRB::Notifier::LeveledNotifier]
Comparison operators can be used in objects of all the above classes, as in the following examples.
# String
"a" < "b" # Output: => true
"a" > "b" # Output: => false
# Symbol
:a < :b # Output: => true
:a > :b # Output: => false
# Fixnum (subclass of Integer)
1 < 2 # Output: => true
2 >= 2 # Output: => true
# Float
1.0 < 2.0 # Output: => true
2.0 >= 2.0 # Output: => true
# Time
Time.local(2016, 5, 28) < Time.local(2016, 5, 29) # Output: => true
Most sorting operations use the spaceship (<=>) operator.