Skip to content

Instantly share code, notes, and snippets.

@kristm
Last active August 29, 2015 14:24
Show Gist options
  • Save kristm/3d26ba1a31860119e92b to your computer and use it in GitHub Desktop.
Save kristm/3d26ba1a31860119e92b to your computer and use it in GitHub Desktop.
group in pairs
#get array of cities by
#Schools.where('XX').map(&:city).uniq.sort { |a,b| a.downcase <=> b.downcase }
def group_in_pairs (arr)
ra = []
skip = -1
arr.each_with_index do |a,i|
return ra if arr[i+1].nil?
next if skip > i
if (a.downcase == arr[i+1].downcase)
ma = []
ni = i+1
ma.push a
while (a.downcase == arr[ni].try(:downcase))
ma.push arr[ni]
ni = ni + 1
end
ra << ma
skip = ni
else
ra << [a.titleize, a] unless a.titleize == a
end
end
ra
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment