Convert alphanum sort from comparitor to key operator
[htsworkflow.git] / htsworkflow / util / alphanum.py
1 # from http://stackoverflow.com/questions/4836710/does-python-have-a-built-in-function-for-string-natural-sort
2 # modified by Diane Trout
3
4 import re
5
6 def natural_sort_key(s, _nsre=re.compile('([0-9]+)')):
7     if isinstance(s, type("")) or isinstance(s, type(u"")):
8         return [int(text) if text.isdigit() else text.lower()
9                 for text in re.split(_nsre, s)]
10     elif isinstance(s, int):
11         return [s]
12     else:
13         raise ValueError("Unsupported type %s for input %s" % (type(s), s))