Skip to content

Instantly share code, notes, and snippets.

@cnocon
Last active December 11, 2015 02:59
Show Gist options
  • Save cnocon/4534239 to your computer and use it in GitHub Desktop.
Save cnocon/4534239 to your computer and use it in GitHub Desktop.
Sorting exercise (Ch. 10) from "Learning to Program" by Chris Pine (2nd Edition)
puts 'Please enter a list of words, separated by commas, that you would like to sort alphabetically.'
unsorted = gets.chomp.downcase.split(',')
sorted = []
def sorter unsorted, sorted
if unsorted.length == 0
return sorted
else
smalls = unsorted.pop #Biggie, Biggie, Biggie, can't you see? Sometimes your words just hypnotize me...
unsorted.each do |tester|
if tester < smalls
unsorted.push smalls
smalls = tester
else
unsorted.push tester
sorted.push smalls
end
end
end
sorter unsorted, sorted
end
puts ' '
puts 'Here\'s your alphabetically sorted list! It\'s the bees knees.'
puts unsorted.sort
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment