Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
March 30th, 2004, 11:56 PM
 Questioner
Contributing User

Join Date: Jul 2003
Location: Questioner's home
Posts: 89
Time spent in forums: 8 h 32 m 10 sec
Reputation Power: 11
Sort using psql or python? Error!

I'm retrieving some fields using psql, sort it and then display it in python, I have some problem, and my query's something like this :

"select blah blah, round(sum(something)) from somewhere group by blah blah order by round(sum(something));"

And when I display the result, something's not quite right, the results're not sorted in order, it showed something similar to this :

result
-------
100.00
101.10
106.3
105.5
...

As you can see, some are in order, and some not, what seems to be the problem?

I tried not to use order by and sort the results using list sorting in python, and the outcome is the same.
__________________
What can change the nature of a man?

#2
March 31st, 2004, 02:07 AM
 Grim Archon
Mini me.

Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 13
They are being sorted as strings. If you convert the values to numeric and then sort, the order will be as you expect.

Note that the floating point numbers will not preserve well after the conversion - so you may need to do some formatting back to strings:
Code:
```>>> a = ['100.00','101.10','106.3','105.5']
>>> a
['100.00', '101.10', '106.3', '105.5']
>>> c = map(float,a)
>>> c
[100.0, 101.09999999999999, 106.3, 105.5]
>>> c.sort()
>>> c
[100.0, 101.09999999999999, 105.5, 106.3]
>>> ```

Grim
__________________
*** Experimental Python Markup CGI V2 ***

Last edited by Grim Archon : March 31st, 2004 at 02:11 AM.

#3
March 31st, 2004, 12:20 PM
 jimmy2k1
Contributing User

Join Date: Mar 2002
Posts: 89
Time spent in forums: 8 h 48 m 50 sec
Reputation Power: 12
are you sorting them using your MySQL query or are you using Python to sort them? if you're using MySQL, check to make sure that it's grouping them correctly before Python prints them out.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Sort using psql or python? Error!