Method: Array#insertion_sort_by!

Defined in:
lib/array/sort/insertion_sort.rb

#insertion_sort_by!(&_block) ⇒ Array, Enumerator

Sorts self in place with insertion sort algorithm, using a set of keys generated by mapping the values in self through the given block.

The result is guaranteed to be stable. When the comparison of two elements returns 0, the order of the elements will be preserved.

If no block is given, an Enumerator is returned instead.

Returns:

  • (Array)

    if a block is given, the sorted array

  • (Enumerator)

    if no block is given, an Enumerator



69
70
71
72
73
74
75
76
77
# File 'lib/array/sort/insertion_sort.rb', line 69

def insertion_sort_by!(&_block)
  if block_given?
    insertion_sort! do |a, b|
      yield(a) <=> yield(b)
    end
  else
    to_enum :insertion_sort_by!
  end
end