Ok, I’ve got a list like this (just a sample of data):
data = {"NAME": "James", "RANK": "3.0", "NUM": "27.5" ... }
Now, if I run something like this:
sortby = "NAME" //this gets passed to the function, hence why I am using a variable sortby instead
data.sort(key=itemgetter(sortby))
I get all the strings sorted properly – alphabetically.
However, when “sortby” is any of the floating values (RANK or NUM or any other), sort is done again, alphabetically, instead of numerically, so my sorted list looks something like this then:
0.441
101.404
107.558
107.558
108.48
108.945
11.195
12.143
12.801
131.73
which is obviously wrong.
Now, how can I do a sort like that (most efficiently in terms of speed and resources/computations taken) but have it cast the value somehow to float when it’s a float, and leave it as a string when it’s a string… possible? And no, removing quotes from float values in the list is not an option – I don’t have control over the source list, unfortunately (and I know, that would’ve been an easy solution).
If you want a general function which you can pass as a parameter to
sort(key=XXX), then here’s a candidate complete with test:When you run the above script, you get: