def apply_natural_sort(collection, key=None):
 to_digit = (lambda i: (int(i) if i.isdigit() else i))
 def tokenize_and_convert(item, key=None):
  if key:
   item = item[key]
  return [to_digit(c) for c in re.split('([0-9]+)', item)]
 return sorted(collection, key=(lambda i: tokenize_and_convert(i, key=key)))

def apply_natural_sort ( collection , key = None ) : to_digit = ( lambda i : ( int ( i ) if i . isdigit ( ) else i ) ) def tokenize_and_convert ( item , key = None ) : if key : item = item [ key ] return [ to_digit ( c ) for c in re . split ( '([0-9]+)' , item ) ] return sorted ( collection , key = ( lambda i : tokenize_and_convert ( i , key = key ) ) )
applies a natural sort to a list or dictionary dictionary types require a sort key to be specified .