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 May 7th, 2003, 05:57 PM
ironchef ironchef is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Cleveland
Posts: 112 ironchef User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 14 m 34 sec
Reputation Power: 6
sorting

Hey all

How does sorting work? I saw this function but i have no idea how it works.

python: items.sort( lambda a,b:cmp(a.Title(), b.Title()) )

Thanks for any help.

Reply With Quote
  #2  
Old May 30th, 2003, 10:17 AM
lazy_yogi lazy_yogi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 325 lazy_yogi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 58 m 36 sec
Reputation Power: 6
sort can take in a comparison function.
The comparison function takes in 2 arguments and returns them in sorted form.

Quote:

Eg.
def copare(e1,e2):
return e1-e2


Eg. this will return a negative number to indicate it is to be reversed to sort it

Quote:

compare ('this', 'that')


So to sort the array called 'items', you would use
Quote:

items.sort(compare)


The only diff in ur code is that they used a 'lambda function' which is a function but without a name.) My compare function has the name 'compare' associated to the funtion object.
I pass the function into the sort function as an argument. In their code, they create an anonymous function there and then to pass in as an argument. So their code could be re-written as:

Quote:

def mycompare(a,b):
cmp(a.Title(), b.Title())

items.sort( mycompare )

Last edited by lazy_yogi : May 30th, 2003 at 10:21 AM.

Reply With Quote
  #3  
Old May 30th, 2003, 11:22 AM
ironchef ironchef is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2002
Location: Cleveland
Posts: 112 ironchef User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 14 m 34 sec
Reputation Power: 6
got it!

Thanks for you help, I appreciate it.

B.A.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > sorting


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 2 hosted by Hostway