http://www.perlmonks.org?node_id=1083421

torres09 has asked for the wisdom of the Perl Monks concerning the following question:

hey monks

I have an associative array . So how can i Sort an associative array contents based on its key value...and print the sorted array

thanks in advance

Replies are listed 'Best First'.
Re: sorting an associated array
by tobyink (Canon) on Apr 23, 2014 at 19:29 UTC

    We don't call them associative arrays in Perl. They're called hashes.

    Hashes are inherently unsorted. You can't sort them. You can however get a list of the keys for the hash, and sort that list. The keys function will give you the keys for a hash; the sort function sorts lists.

    my %data = ( c => 3, b => 1, a => 2, ); print "Sorted by key...\n"; for my $key (sort keys %data) { print "$key : $data{$key}\n"; } print "Sorted by value...\n"; for my $key (sort { $data{$a} <=> $data{$b} } keys %data) { print "$key : $data{$key}\n"; }
    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re: sorting an associated array
by toolic (Bishop) on Apr 23, 2014 at 20:03 UTC
    See also the FAQ from your command prompt:
    perldoc -q sort
Re: sorting an associated array
by hippo (Bishop) on Apr 23, 2014 at 23:03 UTC

    By reading and understanding the FAQ, of course!

Re: sorting an associated array
by vinoth.ree (Monsignor) on Apr 23, 2014 at 20:49 UTC

    Sort Hash by values. Unlike hash keys, hash values are not guaranteed to be unique. If you sort a hash by only its values, the sort order of two elements with the same value may change when you add or delete other values. To do a stable sort by hash values, do a primary sort by value and a secondary sort by key:

    %hash = ( Elliot => Babbage, Charles => Babbage, Grace => Hopper, Herman => Hollerith ); @sorted = map { { ($_ => $hash{$_}) } } sort { $hash{$a} cmp $hash{$b} or $a cmp $b } keys %hash; foreach $hashref (@sorted) { ($key, $value) = each %$hashref; print "$key => $value\n"; }
    Charles => Babbage
    Elliot => Babbage
    Herman => Hollerith
    Grace => Hopper
    
    Numerical Sort.
    my %data = (bananas => 1,oranges => 7,apples => 12, mangoes => 3,pears + => 8,); # Using <=> instead of cmp because of the numbers foreach my $fruit (sort {$data{$a} <=> $data{$b}} keys %data) { print $fruit . ": " . $data{$fruit} . "\n"; }

    All is well
      Hmm, although it is indeed a bit ambiguous, my understanding of the requirement is to sort the hash on the values of the keys, not on the values of the hash for such keys.
Re: sorting an associated array
by sundialsvc4 (Abbot) on Apr 23, 2014 at 20:31 UTC

    As an aside, I once worked with a group that liked to tie() hashes to Berkely-DB files with millions of records in each.   (Dunno, I guess they mistrusted SQL?)   Anyway, they would do things like that, and the fairly-limited systems would choke because all of those keys had to be pulled out into memory in order to be sorted.   I observed that the each() function when applied to such files would automagically walk the index-tree and thus produce the keys in ascending order, eliminating what was a very costly process.

    If you must produce keys in sorted order, this process will work as described.   However, if the hashes in question are large, and particularly if they are ties, be very aware of what you are actually asking the computer to do.