Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Sorting on multiple fields

Posted on 1999-07-21
Medium Priority
Last Modified: 2010-03-04
I have the O'Reily Programming Perl book (2nd Ed.) that has sample code for sorting on multiple fields.  The example is on page 218.  I'll include the example here too.

sub prospects {
  $money{$b} <=> $money{$a}
  $height{$b} <=> $height{$a}
  $age{$a} <=> $age{$b}
  $lastname{$a} <=> $lastname{$b}
  $a cmp $b;
@sortedclass = sort prospects @class;

I understand what this code is supposed to do but I can't figure out the format of the data.  Can someone explain what @class looks like.  Is it some kind of array of hash tables?

I need to sort a set of records based on multiple fields and print out the list of records in order.  Any help with this would be appreciated.
Question by:themask
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 84

Expert Comment

ID: 1214321
@class=qw(aa bb cc dd ee ff);
@lastname{@class}=(1,2,3,1,2,3);  #are you sure this is numeric?

Accepted Solution

samay earned 200 total points
ID: 1214322
@class is just an array of name of persons or any datastructure that refers to the persons.
@class = qw(john marry);
@class = qw($a $b);
Then there are different hashes which has above array elements as there keys and they have some assigned values which are used in sorting..

I checked the book and lastname comparision uses "cmp"

Let's say your records are


in a file "in.txt";
Following code will be helpful
open IN, "in.txt";
@lines = <IN>;
shift @lines;  #to remove first line..

Now You have complete data in @lines.
You can create the hashes
foreach (@lines){
  @values = split /\,/;
  $person = shift @values;  
  ($money{$person},$height{$person},$age{$person},$lastname{$person}) = @values;
  push @class, $person;
Now you can use the subroutine that is provided in the book.
@sortedclass = sort prospects @class

To Print the records
foreach $person (@sortedclass){
    print "$person,$money{$person},$height{$person},$age{$person},$lastname{$person}\n";



Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

660 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question