Class: Array
- Inherits:
-
Object
- Object
- Array
- Defined in:
- lib/nutritional_calculator/sort_methods.rb
Instance Method Summary collapse
Instance Method Details
#sort_using_each ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/nutritional_calculator/sort_methods.rb', line 20 def sort_using_each copy = [] self.each do |element| index = (0...copy.size).bsearch { |n| copy[n] > element } if index copy.insert(index, element) else copy << element end end copy end |
#sort_using_for ⇒ Object
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/nutritional_calculator/sort_methods.rb', line 4 def sort_using_for sorted_array = self.dup for i in 1...sorted_array.length do for j in (i..sorted_array.length-1).to_a.reverse do if sorted_array[j] < sorted_array[j-1] tmp = sorted_array[j-1] sorted_array[j-1] = sorted_array[j] sorted_array[j] = tmp end end end sorted_array end |