Class: SortedArray

Inherits:
Array
  • Object
show all
Defined in:
lib/gepub/sorted_array.rb

Instance Method Summary collapse

Methods inherited from Array

#sort_as_meta

Constructor Details

#initialize(*args, &sort_by) ⇒ SortedArray

Returns a new instance of SortedArray.



2
3
4
5
6
# File 'lib/gepub/sorted_array.rb', line 2

def initialize(*args, &sort_by)
  @sort_by = sort_by || Proc.new { |x,y| x <=> y }
  super(*args)
  self.sort!() &sort_by
end

Instance Method Details

#<<(v) ⇒ Object Also known as: push, unshift



13
14
15
# File 'lib/gepub/sorted_array.rb', line 13

def <<(v)
  insert(0, v)
end

#insert(_i, v) ⇒ Object



8
9
10
11
# File 'lib/gepub/sorted_array.rb', line 8

def insert(_i, v)
  insert_before = index(find { |x| @sort_by.call(x, v) == 1 })
  super(insert_before ? insert_before : -1, v)
end