Hash Sorting

Discussion in 'Perl Misc' started by Brad, Jun 14, 2005.

  1. Brad

    Brad Guest

    I have a hash like this:

    $HASH{'key'}{'date'} = some date
    $HASH{'key'}{'status'} = some status

    I'd like to sort and display this by date:

    key | date | status

    I cannot seem to find any references to sorting this type of hash in this
    way. I need some assistance finding some documentation, or a copy/paste of
    the code for this type of sort.

    Thanks for any help,
    Brad
     
    Brad, Jun 14, 2005
    #1
    1. Advertisements

  2. On Tue, 14 Jun 2005 13:49:56 -0700, Brad wrote:

    > I have a hash like this:
    >
    > $HASH{'key'}{'date'} = some date
    > $HASH{'key'}{'status'} = some status
    >
    > I'd like to sort and display this by date:
    >
    > key | date | status
    >
    > I cannot seem to find any references to sorting this type of hash in this
    > way. I need some assistance finding some documentation, or a copy/paste of
    > the code for this type of sort.
    >

    You can do it with a double map and sort.

    Google for

    "schwarzian transform" perl

    Mark
     
    Mark Clements, Jun 14, 2005
    #2
    1. Advertisements

  3. Brad

    Brad Guest

    > You can do it with a double map and sort.
    >
    > Google for
    >
    > "schwarzian transform" perl
    >
    > Mark


    Thanks Mark... kept searching, after my post, and I found the answer:

    foreach my $spc ( sort { $hoh{$a}->{'cpu'} <=> $hoh{$b}->{'cpu'} } keys
    %hoh) {
    print $spc, " has got ", ${hoh}{$spc}{cpu}, "\n";
    }
     
    Brad, Jun 14, 2005
    #3
  4. Brad

    Anno Siegel Guest

    Mark Clements <> wrote in comp.lang.perl.misc:
    > On Tue, 14 Jun 2005 13:49:56 -0700, Brad wrote:
    >
    > > I have a hash like this:
    > >
    > > $HASH{'key'}{'date'} = some date
    > > $HASH{'key'}{'status'} = some status
    > >
    > > I'd like to sort and display this by date:
    > >
    > > key | date | status
    > >
    > > I cannot seem to find any references to sorting this type of hash in this
    > > way. I need some assistance finding some documentation, or a copy/paste of
    > > the code for this type of sort.
    > >

    > You can do it with a double map and sort.
    >
    > Google for
    >
    > "schwarzian transform" perl


    Why do you think that's useful?

    A Schwartzian transform is an efficiency hack that saves time when the
    extraction of sort keys from the elements is expensive. It doesn't
    facilitate any particular ordering, you'll have to do that either way.

    Since the sort keys are available through hash access, there isn't much
    a Schwartzian can gain in this case.

    Anno
     
    Anno Siegel, Jun 15, 2005
    #4
  5. On 15 Jun 2005 09:40:46 GMT, Anno Siegel wrote:

    > Mark Clements <> wrote in comp.lang.perl.misc:
    >> On Tue, 14 Jun 2005 13:49:56 -0700, Brad wrote:
    >>
    >>> I have a hash like this:
    >>>
    >>> $HASH{'key'}{'date'} = some date
    >>> $HASH{'key'}{'status'} = some status
    >>>
    >>> I'd like to sort and display this by date:
    >>>
    >>> key | date | status
    >>>
    >>> I cannot seem to find any references to sorting this type of hash in this
    >>> way. I need some assistance finding some documentation, or a copy/paste of
    >>> the code for this type of sort.
    >>>

    >> You can do it with a double map and sort.
    >>
    >> Google for
    >>
    >> "schwarzian transform" perl

    >
    > Why do you think that's useful?

    It demonstrates passing different comparison operations to sort, and the
    use of multiple maps, though I didn't realise that the Schwarzian transform
    was specific to non-prekeyed data.

    >
    > A Schwartzian transform is an efficiency hack that saves time when the
    > extraction of sort keys from the elements is expensive. It doesn't
    > facilitate any particular ordering, you'll have to do that either way.


    > Since the sort keys are available through hash access, there isn't much
    > a Schwartzian can gain in this case.

    Fair enough, but the suggestion was intended more as a learning exercise
    than as a "do it this way".

    Mark
     
    Mark Clements, Jun 15, 2005
    #5
    1. Advertisements

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. Replies:
    2
    Views:
    1,745
    James Kanze
    Jul 6, 2010
  2. Jason
    Replies:
    0
    Views:
    750
    Jason
    Oct 4, 2006
  3. rp
    Replies:
    1
    Views:
    838
    red floyd
    Nov 10, 2011
  4. Williams, Chris
    Replies:
    3
    Views:
    229
    Florian Gross
    Dec 13, 2004
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    969
    David A. Black
    Jul 2, 2008
  6. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    786
    Michael Budash
    Oct 11, 2003
  7. IanW
    Replies:
    3
    Views:
    229
    Ian Stuart
    Dec 14, 2005
  8. Íéêüëáïò Êïýñáò

    Sorting a set works, sorting a dictionary fails ?

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