I'm currently trying to sort a list of the form:
[["Chr1", "949699", "949700"],["Chr11", "3219", "444949"],
["Chr10", "699", "800"],["Chr2", "232342", "235345234"],
["ChrX", "4567", "45634"],["Chr1", "950000", "960000"]]
Using the built-in sorted()
, I get:
[['Chr1', '949699', '949700'], ['Chr1', '950000', '960000'], ['Chr10', '699', '800'], ['Chr11', '3219', '444949'], ['Chr2', '232342', '235345234'], ['ChrX', '4567', '45634']]
but I want "Chr2" to come before "Chr10". My current solution involves some code adapted from the page: Does Python have a built in function for string natural sort?
My current solution looks like this:
import re
def naturalSort(l):
convert= lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key= lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
if isinstance(l[0], list):
return sorted(l, key= lambda k: [alphanum_key(x) for x in k])
else:
return sorted(l, key= alphanum_key)
Yielding the correct order:
[['Chr1', '949699', '949700'], ['Chr1', '950000', '960000'], ['Chr2', '232342', '235345234'], ['Chr10', '699', '800'], ['Chr11', '3219', '444949'], ['ChrX', '4567', '45634']]
Is there a better way to do this?