class Array
  def sort_index
    d=[]
    self.each_with_index{|x,i| d[i]=[x,i]}
     if block_given?
       d.sort {|x,y| yield x[0],y[0]}.collect{|x| x[1]}
     else
       d.sort.collect{|x| x[1]}
     end
  end
  def sort_with(ord=[])
     return nil if self.length!=ord.length
     self.values_at(*ord)
  end
end

a = [21, 33, 11, 34, 36, 24, 14]
b = a.sort_index
a2 = a.sort_with(b)
c = a.sort_index {|x,y| x%2 <=> y%2 }
a3 = a.sort_with(c)
p a          # [21, 33, 11, 34, 36, 24, 14]
p b          # [2, 6, 0, 5, 1, 3, 4]
p a2         # [11, 14, 21, 24, 33, 34, 36]
p c          # [6, 5, 4, 3, 2, 1, 0]
p a3         # [14, 24, 36, 34, 11, 33, 21]

