I've got a list that looks like this:
["A", "X", "B", "3", "5", "1"]
I'm trying to find a way to sort this so it looks like this:
["X", "B", "A", 1, 3, 5]
I created a small function to convert the list strings to integer where appropriate:
def convert_list(list_to_convert: list) -> list:
"""If a list contains strings where some should be ints, this attempts to
convert them to ints where appropriate"""
converted_list = []
for item in list_to_convert:
try:
item = int(item)
except ValueError:
pass
converted_list.append(item)
return converted_list
That gives me ["A", "X", "B", 3, 5, 1]
But I'm not sure how to get this list to sort by the letters in descending, while sorting the integers in ascending.
I've tried this:
sorted_int_grade_list = sorted(
ordered_grade_list, key=lambda i: (isinstance(i, int), i)
)
But that gives me ["A", "B", "X", 1, 3, 5]
(alpha is wrong direction, integers are correct)
I'm not sure how to sort this in two different fashions - any ideas would be helpful.