import re


def naturallysorted(L, reverse=False):
  """ Similar functionality to sorted() except it does a natural text sort
  which is what humans expect when they see a filename list.
  """
  convert = lambda text: ("", int(text)) if text.isdigit() else (text, 0)
  alphanum = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
  return sorted(L, key=alphanum, reverse=reverse)