Skip to content

Instantly share code, notes, and snippets.

@joel-extremo
Created October 14, 2018 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joel-extremo/dc5c3f2e3697be5c8ff4584d9db52641 to your computer and use it in GitHub Desktop.
Save joel-extremo/dc5c3f2e3697be5c8ff4584d9db52641 to your computer and use it in GitHub Desktop.
def alphabetize(arr, rev=false)
if rev
arr.sort! { |item1, item2| item2 <=> item1 }
else
arr.sort! { |item1, item2| item1 <=> item2 }
end
end
books = ["Heart of Darkness", "Code Complete", "The Lorax", "The Prophet", "Absalom, Absalom!"]
puts "A-Z: #{alphabetize(books)}"
puts "Z-A: #{alphabetize(books, true)}"
puts books
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment