Split an array in ruby based on some condition Split an array in ruby based on some condition ruby ruby

Split an array in ruby based on some condition


Try the partition method

@arr1, @arr2 = @level1.partition { |x| x[0] > 2.1 }

The condition there may need to be adjusted, since that wasn't very well specified in the question, but that should provide a good starting point.


something like this?

arr = [  ["3.1", 4],  ["3.0", 7],  ["2.1", 5],  ["2.0", 6],  ["1.9", 3]]arr1, arr2 = arr.inject([[], []]) do |f,a|  a.first.to_f <= 2.1 ? f.last << a : f.first << a; fendarr = arr1 + arr2.reverse# => [["3.1", 4], ["3.0", 7], ["1.9", 3], ["2.0", 6], ["2.1", 5]]