Skip to content

Instantly share code, notes, and snippets.

@jordelver
Last active September 29, 2015 19:17
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 jordelver/1652309 to your computer and use it in GitHub Desktop.
Save jordelver/1652309 to your computer and use it in GitHub Desktop.
pear
d
c
b
a
banana
c
a
b
apple
a
f
e
apple
a
e
f
banana
a
b
c
pear
a
b
c
d
#!/usr/bin/env ruby
contents = {}
current = ""
# Parse string
ARGF.each do |line|
if line =~ /^\s+/
contents[current] << line
else
current = line.chop
contents[current] = []
end
end
# Sort
output = ""
Hash[contents.sort].each do |key, value|
output << "#{key}\n"
value.sort_by { |word| word.lstrip }.each do |child|
output << child
end
end
puts output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment