I have a list of data that I have to sort, and, unfortunately, the naming scheme for these objects is not very consistent. The data is a list of strings that are most often real numbers, but sometimes have a letter at the end. Some examples of valid values in this list are as follows:
['1', '1.1', '1.2', '2', '2.1A', '2.1B', '2.2A', '101.1', '101.2']
Since they are in the database, my first thought was to use the following django method to return sorted results, but it returns it as follows.
choices = [l.number for l in Locker.objects.extra(
select={'asnumber': 'CAST(number as BYTEA)'}).order_by('asnumber')]
print choices
==> ['1', '1.1', '101.1', '101.2', '2', '2.1A', '2.1B', '2.2A']
Unfortunately, he could not sort it as he should. So my new plan is to write a method that will work with the python method sorted, but I'm still not sure how to do this. I need to find a way to sort by the natural part of the string, and then as a secondary sort, sort by the attached letter to the end.
Any tips on where to go with this?