Method: Immutable::List#sort_by

Defined in:
lib/immutable/list.rb

#sort_by {|element| ... } ⇒ List

Return a new List with the same items, but sorted. The sort order is determined by mapping the items through the given block to obtain sort keys, and then sorting the keys according to their natural sort order (‘#<=>`).

Examples:

Immutable::List["Elephant", "Dog", "Lion"].sort_by { |e| e.size }
# => Immutable::List["Dog", "Lion", "Elephant"]

Yields:

  • (element)

    Once for each element.

Yield Returns:

  • a sort key object for the yielded element.

Returns:



575
576
577
578
# File 'lib/immutable/list.rb', line 575

def sort_by(&transformer)
  return sort unless block_given?
  LazyList.new { List.from_enum(super(&transformer)) }
end