|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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. |
|
#2
|
||||||
|
||||||
|
sort can take in a comparison function.
The comparison function takes in 2 arguments and returns them in sorted form. Quote:
Eg. this will return a negative number to indicate it is to be reversed to sort it Quote:
So to sort the array called 'items', you would use Quote:
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:
Last edited by lazy_yogi : May 30th, 2003 at 10:21 AM. |
|
#3
|
|||
|
|||
|
got it!
Thanks for you help, I appreciate it.
B.A. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > sorting |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|