Method: Immutable::Hash#sort_by

Defined in:
lib/immutable/hash.rb

#sort_by {|key, value| ... } ⇒ Vector

Return a Vector which contains all the ‘[key, value]` pairs in this Hash as two-element Arrays. The order which the pairs will appear in is determined by passing each pair to the code block to obtain a sort key object, and comparing the sort keys using `#<=>`.

Examples:

h = Immutable::Hash["Dog" => 1, "Elephant" => 2, "Lion" => 3]
h.sort_by { |key, value| key.size }
# => Immutable::Vector[["Dog", 1], ["Lion", 3], ["Elephant", 2]]

Yields:

  • (key, value)

    Once for each key/value pair.

Yield Returns:

  • a sort key object for the yielded pair.

Returns:

See Also:

  • Enumerable#sort_by


542
543
544
# File 'lib/immutable/hash.rb', line 542

def sort_by
  Vector.new(super)
end