sorting items in a table problematic because of scientific notation

MRAB google at mrabarnett.plus.com
Tue Apr 28 20:15:26 EDT 2009


Davis, Amelie Y wrote:
> Hi All,
> 
> I have a dbf table outputted by another program that I cannot (I’m 
> pretty sure) change the format of.
> 
> I use a dbf reader code found online 
> (http://code.activestate.com/recipes/362715/ ) to read the table in and 
> I need to sort it on a particular field but this field has scientific 
> notation in it and when I use the following command, it seems to ignore 
> the scientific notation which is very problematic outlist = 
> sorted(records, key=itemgetter(2)) . 
> 
[snip]
The field you're sorting on is actually a string. For example, '1' comes
before '2', so '10' comes before '2'.

If you want to sort by numeric value then you need to get the numeric
value:

     outlist = sorted(records, key=float(itemgetter(2)))



More information about the Python-list mailing list