Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re: hash sorting/alphabetization issue : country postal codes

by grep (Monsignor)
on Jul 03, 2007 at 19:40 UTC ( #624761=note: print w/replies, xml ) Need Help??


in reply to hash sorting/alphabetization issue : country postal codes

Send sort the hash values not the keys.

#!/usr/bin/perl use warnings; use strict; my %country_names = ( "AF" => "Afghanistan", "AX" => "Aland Islands", "AL" => "Albania", "DZ" => "Algeria", "AS" => "American Samoa", "AD" => "Andorra", "AO" => "Angola", "AI" => "Anguilla", "AQ" => "Antarctica", "AG" => "Antigua And Barbuda", "AR" => "Argentina", "AM" => "Armenia", "AW" => "Aruba", "AU" => "Australia", "AT" => "Austria", "AZ" => "Azerbaijan", "BS" => "Bahamas", "BH" => "Bahrain", "BD" => "Bangladesh", "BB" => "Barbados", "ZZ" => "AAAAAAA" ); my @sorted_keys = sort { $country_names{$a} cmp $country_names{$b} } k +eys %country_names; print join("\n",@sorted_keys);

Replies are listed 'Best First'.
Re^2: hash sorting/alphabetization issue : country postal codes
by parv (Priest) on Jul 03, 2007 at 20:24 UTC
    my @sorted_keys = sort { $country_names{$a} cmp $country_names{$b} } k +eys %country_names;

    Considering that in the end you would have sorted VALUES not the keys, above array variable name is rather misleading.

    Correction. Afer grep's reply below, I saw that what was misleading was my claim made above. The array name was reasonable; it was my fault for forgetting that sort sorted the LIST based on result of BLOCK.

      Considering that in the end you would have sorted VALUES not the keys, above array variable name is rather misleading.

      I disagree - @sorted_keys contains SORTED KEYS, sorted by the values.
      But feel free to call it @keys_sorted_by_value if you feel like it.

        Oh! Right you were, and are. Indeed the array name is reasonable and the array does contain sorted keys not the values. I apologise for distrubing the peace.

        I will stand in a corner now for a while ...

Re^2: hash sorting/alphabetization issue : country postal codes
by hmbscully (Scribe) on Jul 03, 2007 at 19:59 UTC
    The hash is already in alphabetical order by the values. I sorted those first thing.

    I learn more and more about less and less until eventually I know everything about nothing.
      Hashes are not ordered (well in any order you care about). So I don't quite know what you are referring to.

      but the problem is that the option items are in alphabetical order based on the country codes, not the country names.

      Am I not understanding what you meant by that?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://624761]
help
Chatterbox?
[shmem]: ns{1,2,3}.nasa.gov unreachable and no caching. -.-
[karlgoethebier]: funny that they call it Big Dipper in the US. Here this part of Ursa Major is called "Großer Wagen" Big wagon...or car as you like ;-)

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2017-11-21 18:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:













    Results (308 votes). Check out past polls.

    Notices?