class SortedArray::DefaultSorter

Sorts by comparing of :field a <=> b.

Descend your Sorter from this class. It must at least define a method 'sort'. @example

class Foo
  def initialize(_bar)
    @bar = _bar
  end
  def bar
    @bar
  end
end
s = DefaultSorter.new(:bar)
a = SortedArray(s, Foo.new(1), Foo.new(3), Foo.new(2))
# => Foo(1), Foo(2), Foo(3)

@see SortedArray

Attributes

method[R]

Public Class Methods

new(_method) click to toggle source

@param [String] _method - the method to be used while sorting.

# File lib/sorted_array/default_sorter.rb, line 25
def initialize(_method)
  @method = _method
end

Public Instance Methods

marshal_dump() click to toggle source

@return [Array] - Classname, methodname

# File lib/sorted_array/default_sorter.rb, line 39
def marshal_dump
  [ self.class, @method ]
end
marshal_load(array) click to toggle source

@param [Array] array - _classname,

# File lib/sorted_array/default_sorter.rb, line 44
def marshal_load array
  @method = array ? array.last : :to_s
end
sort(object) click to toggle source

Sort the given object. @abstract Overwrite this method in descendants to sort your objects for your needs @param [Object] what - what must provice a method sort! and :method @return [Object]

# File lib/sorted_array/default_sorter.rb, line 34
def sort(object)
  object.sort! { |a,b| a.send(@method) <=> b.send(@method) }
end