Skip to content

Instantly share code, notes, and snippets.

@pjbriggs
Created July 22, 2013 12:29
Show Gist options
  • Save pjbriggs/6053476 to your computer and use it in GitHub Desktop.
Save pjbriggs/6053476 to your computer and use it in GitHub Desktop.
Script to explore the use of different types of sort keys, including "natural sort order"
# Compare effects of using different sort keys
import os
import locale
import re
class SortKeys:
natural_sort_digits = re.compile(r'(\d+)')
@classmethod
def default(self,filename):
# Default Python ordering for sort
return filename
@classmethod
def locale(self,filename):
# Ordering using the locale
return locale.strxfrm(filename)
@classmethod
def natural(self,filename):
# Natural order sorting from Pavel Repin, see
# http://stackoverflow.com/a/5997173/579925
return tuple(int(token) if match else token
for token, match in
((fragment, self.natural_sort_digits.search(fragment))
for fragment in self.natural_sort_digits.split(filename)))
def list_files(dirn,sort_key=None):
# Print a list of all files under a directory
files = []
for d in os.walk(dirn):
for f in d[2]:
files.append(os.path.join(str(d[0])[len(dirn):].lstrip(os.sep),f))
files.sort(key=sort_key)
for f in files:
print f
return files
if __name__ == "__main__":
print "# Default ordering"
default_order = list_files('.',sort_key=SortKeys.default)
print "# Locale ordering"
locale_order = list_files('.',sort_key=SortKeys.locale)
print "# Natural ordering"
natural_order = list_files('.',sort_key=SortKeys.natural)
print "Default == locale:\t%s" % (locale_order == default_order)
print "Default == natural:\t%s" % (natural_order == default_order)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment