in reply to Re: Advance Sorting
in thread Advance Sorting

I see thanks for that. I have a list of IDs with a corresponding score and location values. I want to order the IDs based on highest score associated with them. I also want the IDs to be grouped together. When I use array of hashes like below how would I order it then?

push @entry, { ID => 'BLA', Score => 5, Location => '1-10', }; push @entry, { ID => 'TRA', Score => 15, Location => '7-15', }; push @entry, { ID => 'BLA', Score => 10, Location => '2-10', }; Output; TRA 15 7-15 BLA 10 2-10 BLA 5, 1-10

Replies are listed 'Best First'.
Re^3: Advance Sorting
by LanX (Saint) on Jun 03, 2014 at 16:54 UTC
    That's a completely different data structure now!!!

     @sorted = sort { $a->{Score} <=> $b->{Score} } @entry

    Does it.

    Cheers Rolf

    (addicted to the Perl Programming Language)

      If hashes cannot be order I have to change the data structure. My previous example was however quite bad - it's not a simple ordering by Score. I want to order by top Score per ID (BLA, TRA) and then order by score for particular IDs score, grouping them together. Below is a bit better example. Apologies for confusing you and thanks for help;

      my @entry; push @entry, { ID => 'BLA', Score => 5, Location => '1-10', }; push @entry, { ID => 'TRA', Score => 15, Location => '7-15', }; push @entry, { ID => 'BLA', Score => 10, Location => '2-10', }; push @entry, { ID => 'BLA', Score => 35, Location => '90-150', }; Output; BLA 35 90-150 BLA 10 2-10 BLA 5, 1-10 TRA 15 7-15
         
        @sorted = sort { $a->{ID} cmp $b->{ID} # alphanum asc or $b->{Score} <=> $a->{Score} # num. + desc } @entry

        See sort !

        Cheers Rolf

        (addicted to the Perl Programming Language)