Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Sorting a Hash

by LostS (Friar)
on Oct 19, 2001 at 00:18 UTC ( [id://119792]=perlquestion: print w/replies, xml ) Need Help??

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

Hey Everyone
OK The company I am with wants me to accomplish this. Take 4 flat files. Each one contianing the names and info of the employees in that department. They want a search engine. I need to search each file if they select all or search search individual files if they select a group. Now my issue is on sorting. I need to sort first by the last name then by the first name. So say Williams, John will come before Williams, Sam. How would I setup this type os sort criteria where it sorts by last name then sorts by first name... Any suggestions or questions??

-----------------------
Billy S.
Slinar Hardtail - Guildless
Datal Ephialtes - Guildless
RallosZek.Net Admin/WebMaster
Aerynth.Net Admin/WebMaster

perl -e '$cat = "cat"; if ($cat =~ /\143\x61\x74/) { print "Its a cat! +\n"; } else { print "Thats a dog\n"; } print "\n";'

Replies are listed 'Best First'.
Re: Sorting a Hash
by Masem (Monsignor) on Oct 19, 2001 at 00:24 UTC
    my @sorted_keys = sort { $hash{$a}->{last} cmp $hash{$b}->{last} || $hash{$a}->{first} cmp $hash{$b}->{first} } keys %hash;

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    "I can see my house from here!"
    It's not what you know, but knowing how to find it if you don't know that's important

Re: Sorting a Hash
by dragonchild (Archbishop) on Oct 19, 2001 at 00:22 UTC
    Pass a function into sort that sorts on two criteria.

    This kind of problem is directly addressed in a very large number of nodes (doing a SuperSearch for "search" might help) as well as in nearly every Perl book ever printed.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Also check out Schwartzian Transform, which is just the name given to using sort efficiently with complex data structures. Even if you don't use it for this project, you will sooner or later.

      -jackdied

as always, perldoc -q is your friend
by Fletch (Bishop) on Oct 19, 2001 at 07:00 UTC
    $ perldoc -q sort Found in /usr/lib/perl5/5.6.1/pod/perlfaq4.pod How do I sort an array by (anything)? ... How do I sort a hash (optionally by value instead of key)? ... How can I always keep my hash sorted? ...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://119792]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-04-23 23:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found