Method: Daru::Vector#sort

Defined in:
lib/daru/vector.rb

#sort(opts = {}, &block) ⇒ Object

Sorts a vector according to its values. If a block is specified, the contents will be evaluated and data will be swapped whenever the block evaluates to true. Defaults to ascending order sorting. Any missing values will be put at the end of the vector. Preserves indexing. Default sort algorithm is quick sort.

Options

  • :ascending - if false, will sort in descending order. Defaults to true.

  • :type - Specify the sorting algorithm. Only supports quick_sort for now.

Usage

v = Daru::Vector.new ["My first guitar", "jazz", "guitar"]
# Say you want to sort these strings by length.
v.sort(ascending: false) { |a,b| a.length <=> b.length }


643
644
645
646
647
648
649
650
651
652
# File 'lib/daru/vector.rb', line 643

def sort opts={}, &block
  opts = {ascending: true}.merge(opts)

  vector_index = resort_index(@data.each_with_index, opts, &block)
  vector, index = vector_index.transpose

  index = @index.reorder index

  Daru::Vector.new(vector, index: index, name: @name, dtype: @dtype)
end