473,320 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

python 3: sorting with a comparison function (another question)

I read in the following thread that the "cmp" method is not needed for sorting and that "key" is egnough :
http://bytes.com/topic/python/answer...rison-function

but if you need to compare program version, how do you do this with only "key" method ?
Expand|Select|Wrap|Line Numbers
  1. versions = ["3.5", "1.0", "3.1.3", "1.2.0",
  2.             "15.15421.21.42.63.1.0.3.54"]
  3. ordered = sorted(versions, key=???)
  4.  
Thanks.
Sep 4 '12 #1

✓ answered by bvdet

One way is to use functools.cmp_to_key:
Expand|Select|Wrap|Line Numbers
  1. from functools import cmp_to_key
  2.  
  3. def c(a,b):
  4.     a1 = [int(item) for item in a.split(".")]
  5.     b1 = [int(item) for item in b.split(".")]
  6.     for i in range(min(len(a1), len(b1))):
  7.         j = cmp(a1[i], b1[i])
  8.         if j:
  9.             return j
  10.     return cmp(a,b)
  11.  
  12. versions = ["3.5", "1.0", "3.1.3", "1.2.0", "15.15421.21.42.63.1.0.3.54"]
  13.  
  14. versions2 = sorted(versions, key=cmp_to_key(c))
  15.  
  16. print versions2

2 3618
bvdet
2,851 Expert Mod 2GB
One way is to use functools.cmp_to_key:
Expand|Select|Wrap|Line Numbers
  1. from functools import cmp_to_key
  2.  
  3. def c(a,b):
  4.     a1 = [int(item) for item in a.split(".")]
  5.     b1 = [int(item) for item in b.split(".")]
  6.     for i in range(min(len(a1), len(b1))):
  7.         j = cmp(a1[i], b1[i])
  8.         if j:
  9.             return j
  10.     return cmp(a,b)
  11.  
  12. versions = ["3.5", "1.0", "3.1.3", "1.2.0", "15.15421.21.42.63.1.0.3.54"]
  13.  
  14. versions2 = sorted(versions, key=cmp_to_key(c))
  15.  
  16. print versions2
Sep 4 '12 #2
Excellent!
That is just what I need.
Sep 4 '12 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

40
by: Xah Lee | last post by:
is it possible in Python to create a function that maintains a variable value? something like this: globe=0; def myFun(): globe=globe+1 return globe
11
by: David Rasmussen | last post by:
I want to use sort() and supply my own comparison function, like bool lessThan(const S& a, const S& b) { return value(a) < value(b); } and then sort by: sort(a.begin(), a.end(), lessThan);
6
by: aurgathor | last post by:
Howdy, How do I pass some function a generic comparison function? I figured out one non-generic case, but since this code got parameter declarations in two places, it's obviously not generic....
14
by: Spoon | last post by:
Hello, I've come across the following comparison function used as the 4th parameter in qsort() int cmp(const void *px, const void *py) { const double *x = px, *y = py; return (*x > *y) -...
2
by: eastern_strider | last post by:
I'm running into problems about defining a comparison function for a map which has a user defined key. For example: class Key { public: string name; int number; Key (na, nu) : name (na),...
0
by: SvenMathijssen | last post by:
Hi, I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer. What I try to do is...
5
by: fade | last post by:
Good afternoon, I need some advice on the following: I've got a class that has a member std::vector<CStringm_vFileName and a member CString m_path; The vector contains a bunch of filenames with...
18
by: PicO | last post by:
how can i make a set with comparison function ? all i know that i can make a map with comparison function like this struct strCmp { bool operator()( const char* s1, const char* s2 ) const {...
18
by: mdh | last post by:
May I ask the following. By K&R's own admission, the example used to describe function pointers is complex ( on P119). In addition, the use of casts has been stated by some on this group as...
11
by: Thomas Heller | last post by:
Does Python 3 have no way anymore to sort with a comparison function? Both .sort() and sorted() seem to accept only 'key' and 'reverse' arguments, the 'cmp' argument seems to be gone. Can that...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.