Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old March 30th, 2004, 10:56 PM
Questioner Questioner is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Questioner's home
Posts: 89 Questioner User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 32 m 10 sec
Reputation Power: 6
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?

Reply With Quote
  #2  
Old March 31st, 2004, 01:07 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
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 01:11 AM.

Reply With Quote
  #3  
Old March 31st, 2004, 11:20 AM
jimmy2k1 jimmy2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2002
Posts: 89 jimmy2k1 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 8 h 48 m 50 sec
Reputation Power: 7
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Sort using psql or python? Error!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway