Skip to content

Instantly share code, notes, and snippets.

@ndunn219
Last active October 13, 2015 20:05
Show Gist options
  • Save ndunn219/c32bf040f1ae4916477a to your computer and use it in GitHub Desktop.
Save ndunn219/c32bf040f1ae4916477a to your computer and use it in GitHub Desktop.
# import modules
import re
def sort_streets(street_list):
"""
Sort streets alphabetically, ignoring cardinal direction prefixes such as North, South, East and West
:param street_list: list of street names
"""
prefix = re.compile('^North|South|East|West|N\.?|S\.?|E\.?|W\.$', re.IGNORECASE)
# list to store lists for sorting
street_sort_list = []
# for every street
for street in street_list:
street_parts = street.split(' ')
if prefix.match(street_parts[0]) and len(street_parts) > 2:
street_parts.append( street_parts.pop(0) )
street_sort_list.append(street_parts)
# sort the streets
street_sort_list.sort()
sorted_streets = []
for street_parts in street_sort_list:
if prefix.match(street_parts[-1]) and len(street_parts) > 2:
street_parts.insert(0, street_parts.pop())
sorted_streets.append(' '.join(street_parts))
return sorted_streets
@ndunn219
Copy link
Author

This solutions for sorting street names was inspired by Joel McCune's solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment