Q: sort's key and cmp parameters

Discussion in 'Perl Misc' started by gb345, Oct 1, 2009.

  1. gb345

    gb345 Guest

    Challenge: to come up with a sorting task that cannot be achieved
    by passing to the sort method (or sorted function) suitable values
    for its key and reverse parameters, but instead *require* giving
    a value to its cmp parameter.

    For example,

    from random import random
    scrambled = some_list.sort(cmp=lambda x, y: cmp(random(), 0.5))

    can be achieved (to a very good approximation at least) with

    scrambled = some_list.sort(key=lambda x: random())

    Is there a real-life sorting task that requires (or is far more
    efficient with) cmp and can't be easily achieved with key and
    reverse?

    Many thanks in advance,

    G.

    P.S. I guess that, if sort is going to produce a non-trivial,
    *consistent* order, a function CMP passed as the value of its cmp
    parameter must satisfy the following properties:

    0. CMP(x, y) must be non-zero for some elements x, y of the list;
    1. anti-symmetry: sign(CMP(x, y)) must be equal to -sign(CMP(y, x));
    2. transitivity: if sign(CMP(x, y)) equals sign(CMP(y, z)), then
    sign(CMP(x, z)) must be equal to sign(CMP(x, y)).

    In (1) and (2), sign() stands for the function

    -1 if x < 0
    sign(x) = 0 if x == 0
    1 if x > 0

    I suppose that all bets are off if these properties are not satisfied,
    though the documentation does not make this entirely clear, as far
    as I can tell. (If I'm wrong about this alleged omission, please
    point me to the part of the docs that I missed.)
    gb345, Oct 1, 2009
    #1
    1. Advertising

  2. gb345

    kj Guest

    My apologies!

    Please disregard this post. I sent it to the wrong newsgroup.

    G.

    In <ha2ne1$soa$> gb345 <> writes:

    >Challenge: to come up with a sorting task that cannot be achieved
    >by passing to the sort method (or sorted function) suitable values
    >for its key and reverse parameters, but instead *require* giving
    >a value to its cmp parameter.


    >For example,


    >from random import random
    >scrambled = some_list.sort(cmp=lambda x, y: cmp(random(), 0.5))


    >can be achieved (to a very good approximation at least) with


    >scrambled = some_list.sort(key=lambda x: random())


    >Is there a real-life sorting task that requires (or is far more
    >efficient with) cmp and can't be easily achieved with key and
    >reverse?


    >Many thanks in advance,


    >G.


    >P.S. I guess that, if sort is going to produce a non-trivial,
    >*consistent* order, a function CMP passed as the value of its cmp
    >parameter must satisfy the following properties:


    >0. CMP(x, y) must be non-zero for some elements x, y of the list;
    >1. anti-symmetry: sign(CMP(x, y)) must be equal to -sign(CMP(y, x));
    >2. transitivity: if sign(CMP(x, y)) equals sign(CMP(y, z)), then
    > sign(CMP(x, z)) must be equal to sign(CMP(x, y)).


    >In (1) and (2), sign() stands for the function


    > -1 if x < 0
    > sign(x) = 0 if x == 0
    > 1 if x > 0


    >I suppose that all bets are off if these properties are not satisfied,
    >though the documentation does not make this entirely clear, as far
    >as I can tell. (If I'm wrong about this alleged omission, please
    >point me to the part of the docs that I missed.)
    kj, Oct 1, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Damir Mikoc

    CMR/CMP and Compound Primary Key

    Damir Mikoc, Jul 3, 2003, in forum: Java
    Replies:
    1
    Views:
    550
    Christopher Blunck
    Jul 4, 2003
  2. Andrea Sansottera

    no cmp field defined in cmp ejb

    Andrea Sansottera, Jul 16, 2004, in forum: Java
    Replies:
    0
    Views:
    373
    Andrea Sansottera
    Jul 16, 2004
  3. =?ISO-8859-1?Q?Sch=FCle_Daniel?=

    basic questions on cmp, < and sort

    =?ISO-8859-1?Q?Sch=FCle_Daniel?=, Oct 25, 2006, in forum: Python
    Replies:
    4
    Views:
    302
    Fredrik Lundh
    Oct 26, 2006
  4. Replies:
    7
    Views:
    722
    Stefan Arentz
    Sep 10, 2007
  5. kj
    Replies:
    45
    Views:
    1,217
    Raymond Hettinger
    Oct 7, 2009
Loading...

Share This Page