Method: Array#sort_order

Defined in:
lib/rtkit/ruby_extensions.rb

#sort_orderObject

Returns an ordered array of indices, where each element contains the index in the original array which needs to be extracted to produce a sorted array. This method is useful if you wish to sort multiple arrays depending on the sequence of elements in a specific array.

Examples

a = [5, 2, 10, 1]
a.sort_order
=> [3, 1, 0, 2]


129
130
131
132
133
134
135
136
137
# File 'lib/rtkit/ruby_extensions.rb', line 129

def sort_order
  d=[]
  self.each_with_index{|x,i| d[i]=[x,i]}
  if block_given?
    return d.sort {|x,y| yield x[0],y[0]}.collect{|x| x[1]}
  else
    return d.sort.collect{|x| x[1]}
  end
end