Posted By

jatkins on 06/30/10


Tagged

sort array arrays sorting order multidimensionalarrays multidimensionalarray


Versions (?)

Sort one array by another


 / Published in: Ruby
 

Public domain. Return a multidimensional array of array_to_sort and second_array, sorted by the values of array_to_sort.

  1. def sort_array_by_other_array(array_to_sort, second_array)
  2. combined_array = Array.new
  3. i = 0
  4. for sortable_value in array_to_sort
  5. combined_array[combined_array.size] = [sortable_value, second_array[i]]
  6. i += 1
  7. end
  8. combined_array = combined_array.sort { |x,y| x <=> y }
  9. return combined_array
  10. end

Report this snippet  

You need to login to post a comment.