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

x
?
Solved

Sorting on multiple fields

Posted on 1999-07-21
2
Medium Priority
?
241 Views
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}
    or
  $height{$b} <=> $height{$a}
    or
  $age{$a} <=> $age{$b}
    or
  $lastname{$a} <=> $lastname{$b}
    or
  $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.
0
Comment
Question by:themask
[X]
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
2 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 1214321
@class=qw(aa bb cc dd ee ff);
@money{@class}=(22,22,22,11,11,11);
@height{@class}=(150,150,160,160,150,160);
@age{@class}=(9,10,9,10,9,10);
@lastname{@class}=(1,2,3,1,2,3);  #are you sure this is numeric?
0
 

Accepted Solution

by:
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);
or
@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"
operator..

Let's say your records are

person,money,height,age,lastname
A,300,165,25,smith
B,200,170,30,brown
C,400,110,12,williams

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
say
-----------------------------------
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";
}
-------------------------------------------------
   



   





0

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