Sorting in Ruby


/ Published in: Ruby
Save to your folder(s)

The items commented out were the way that things would have had to been coded if there was no
sort!
function in ruby


Copy this code and paste it in your HTML
  1. class Inputstuff
  2. def initialize
  3. @a = 0
  4. @array = Array.new
  5. @counter = 0
  6. end
  7.  
  8. def receive
  9. @a = gets
  10. @a = @a.to_i
  11. while @a >= 0 do
  12. @array << @a
  13. @counter += 1
  14. @a = gets
  15. @a = @a.to_i
  16. end
  17. end
  18.  
  19. def sort
  20. # 0.upto(@counter-2) do |coolstuff|
  21. # if @array[coolstuff] > @array[coolstuff+1]
  22. # @array[coolstuff], @array[coolstuff+1] = @array[coolstuff+1], @array[coolstuff]
  23. # end
  24. # @array.reverse
  25. # end
  26.  
  27. @array.sort! {|x,y| y <=> x}
  28. end
  29.  
  30. def results
  31. @counter.times do |coolerstuff|
  32. puts @array[coolerstuff]
  33. end
  34. end
  35. end
  36.  
  37. i = Inputstuff.new
  38. i.receive
  39. i.sort
  40. i.results

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.