Help with sorting...

Discussion in 'Perl' started by Joe Trubisz, Feb 16, 2004.

  1. Joe Trubisz

    Joe Trubisz Guest

    Hi...

    I have something like this:

    $rec = {};
    $rec->{a} = 1;
    $rec->{b} = 4;
    $D{$rec->{a}}=$rec;
    $rec = {};
    $rec->{a} = 2;
    $rec->{b} = 1;
    $D{$rec->{a}}=$rec;
    $rec = {};
    $rec->{a} = 3;
    $rec->{b} = 3;
    $D{$rec->{a}}=$rec;

    What I want to do is sort D based on the value of b.

    Can someone explain to me what the sort command should look like?
    I'm stumped!

    TIA,
    Joe
    Joe Trubisz, Feb 16, 2004
    #1
    1. Advertising

  2. Joe Trubisz

    Joe Smith Guest

    Joe Trubisz wrote:

    > $rec = {};
    > $rec->{a} = 1;
    > $rec->{b} = 4;
    > $D{$rec->{a}}=$rec;
    > $rec = {};
    > $rec->{a} = 2;
    > $rec->{b} = 1;
    > $D{$rec->{a}}=$rec;
    > $rec = {};
    > $rec->{a} = 3;
    > $rec->{b} = 3;
    > $D{$rec->{a}}=$rec;
    >
    > What I want to do is sort D based on the value of b.


    print "$D{$_}{a} $D{$_}{b}\n" for sort {$D{$a}{b} cmp $D{$b}{b}} keys %D;

    -Joe
    Joe Smith, Feb 16, 2004
    #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. Aric Levin

    DataSet Sorting Problem - HELP!

    Aric Levin, Oct 20, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    1,148
    Natty Gur
    Oct 21, 2003
  2. Replies:
    2
    Views:
    1,405
    James Kanze
    Jul 6, 2010
  3. Jason
    Replies:
    0
    Views:
    372
    Jason
    Oct 4, 2006
  4. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    464
    Michael Budash
    Oct 11, 2003
  5. Íéêüëáïò Êïýñáò

    Sorting a set works, sorting a dictionary fails ?

    Íéêüëáïò Êïýñáò, Jun 10, 2013, in forum: Python
    Replies:
    12
    Views:
    139
    Ulrich Eckhardt
    Jun 10, 2013
Loading...

Share This Page