I am trying to sort a list:
[
'[fc] EDW Ratio (10 degrees)',
' [fc] EDW Ratio (45 degrees)',
' [fc] EDW Ratio (60 degrees)',
' [fc] EDW Ratio (25 degrees)',
' [fc] EDW Ratio (20 degrees)',
' [fc] EDW Ratio (30 degrees)',
' [fc] EDW Ratio (15 degrees)',
' [fc] EDW output factor (60 degrees)',
' [fc] Quality index'
]
using the first part of the accepted answer here:
But the list is ending up like this:
[
' [fc] EDW Ratio (15 degrees)',
' [fc] EDW Ratio (20 degrees)',
' [fc] EDW Ratio (25 degrees)',
' [fc] EDW Ratio (30 degrees)',
' [fc] EDW Ratio (45 degrees)',
' [fc] EDW Ratio (60 degrees)',
' [fc] EDW output factor (60 degrees)',
' [fc] Quality index',
'[fc] EDW Ratio (10 degrees)'
]
whereas I want EDW Ratio (10 degrees) to end up at the start of the list after sorting (index position 0).
How can this be done?
My code includes the following:
#
# Method to define natural sorting used to sort lists
#
def atoi(text):
return int(text) if text.isdigit() else text
def natural_keys(text):
'''
alist.sort(key=natural_keys) sorts in human order
http://nedbatchelder.com/blog/200712/human_sorting.html
(See Toothy's implementation in the comments)
'''
return [ atoi(c) for c in re.split(r'(\d+)', text) ]
.
.
.
tname_list = test_names.split(",") # this outputs the exact first (unsorted) list shown above
tname_list.sort(key=natural_keys) # use human sorting defined above. This outputs the second list shown above.