Welcome to WebProNews Breaking eBusiness and Search News
Advertise | Newsletter | Sitemap | News Feeds News Feed 
 WebProNews Search Part of the iEntry network iEntry inc. 

More Perl Sorting

A.P. Lawrence
Expert Author
Published: 2004-08-03

WebProNews RSS Feed

Perl has an easy to use "sort" function. For example, you might have an array like this:


To list that out alphabetically, you could do:


foreach (sort @month) {
  print "$_n";

If you wanted it reversed, just do

foreach (reverse sort @month) {
  print "$_n";

By the way, "reverse" isn't just for sorting. You could print that array starting at December and ending with January with

foreach (reverse @month) {
  print "$_n";

But suppose the array was more difficult:


You want that sorted, but you want to ignore case. This will work:

foreach (sort { lc($a) cmp lc($b) } @stuff) {
  print "$_n";

That works because Perl lets you specify a code block to help its sort. The code you provide always gets two elements of your array, and will see them as $a and $b. Your code simply decides how $a and $b compare, and returns 1 if $a is greater, 0 if they are equal, and -1 if $a is less than $b.

If the array is numeric, sort by itself doesn't work well:

foreach (sort @stuff) {
  print "$_n";

That would produce:


Using a code block fixes that:

foreach (sort { $a <=> $b } @stuff) {
  print "$_n";

You can replace the code block with a subroutine if you need to. For example, the Consultants List is stored in a file where each line looks like this:

- U.S.A. -|Massachusetts|A.P. Lawrence|Sharon|(781) 784-7547|(781)
658-2012|Tony Lawrence||http://www.pcunix.com/services.html|Sales,
Service SCO Unix, Linux, Macintosh OS X, Mitel SME server (email,
virus scan, web access control, firewwall, spam control,

I want to sort that list, but by very special rules. First, I want to sort by country and "state" within the country, and then by the very last field, which is the update date. If the record hasn't been updated, I want that to sort under records which have been updated, and I only want to compare the year, not the month and day. After that, I want listings that have a company name to appear before those that don't. It's a pretty complicated sort, but Perl can handle it:

@scons=;close CONSULTS;

foreach (sort consort @scons) {



By saying "sort consort", instead of putting code between brackets, Perl sort will call our "consort" subroutine:

sub consort {
# save our variables because our sort routine affects them. If I "chomp $a"
# that will actually change the line seen in the foreach loop that calls this.

chomp $aa;
chomp $bb;

$aa=~ s/^ *//;
$bb=~ s/^ *//;
# split up our fields
$email,$web,$notes,$adate,$junk)=split /|/,$aa;

$bemail,$bweb,$bnotes,$badate,$bjunk)=split /|/,$bb;

# isolate the update date
@adate=split ///,$adate;
@bdate=split ///,$badate;
$acdate="00" if not $acdate;
$bcdate="00" if not $bcdate;
# and the company name
$company="ZZZZZ" if not $company;
$bcompany="ZZZZZ" if not $bcompany;
uc($country) cmp uc($bcountry) || uc($state) cmp uc($bstate) ||
$bcdate <=> $acdate || uc($company) cmp uc($bcompany)
|| uc($aa) cmp uc($bb);


Notice that "$bcdate <=> $acdate"? That effectively reverses the sort. Ordinarily, you would compare the "$a" value to the "$b" value, but by doing it backwards, we reverse that part of the sort, so "04" is listed before "03" and of course "00" comes last. Or'ing together the rest of our sort criteria produces the ordering I want. The very final "uc($aa) cmp uc($bb)" just picks up any differences should all the other criteria be equal.

Receive Our Daily Email of Breaking eBusiness News

About the Author:
A.P. Lawrence provides SCO Unix and Linux consulting services http://www.pcunix.com

WebProNews RSS Feed

More Articles

Contact WebProNews


Targeted Information for Business
WebProNews is part of the iEntry network

Internet Business: Marketing: Small Business:
WebProNews MarketingNewz SmallBusinessNewz
WebProWorld AdvertisingDay PromoteNews
EcommNewz SalesNewz EntrepreneurNewz

Software: Search Engines: Web Design:
WebMasterFree Jayde B2B DesignNewz
NetworkingFiles SearchZA FlashNewz
SecurityConfig SearchNewz WebSiteNotes

Developer: IT Management: Security:
DevWebPro ITManagement SecurityProNews
DevNewz SysAdminNews SecurityConfig
TheDevWeb NetworkingFiles NetworkNewz

The iEntry Network consists of over 100 web publications reaching millions of Internet Professionals. Contact us to advertise.

 Advertise | Contact Us | Corporate | Newsletter | Sitemap | Submit an Article | News Feeds
 WebProNews is an iEntry, Inc. ® publication - $line) { echo $line ; } ?> All Rights Reserved
About WebProNews
WebProNews is the number one source for eBusiness News. Over 5 million eBusiness professionals read WebProNews and other iEntry business and tech publications.

WebProNews provides real-time coverage of internet business.

Free Email Newsletters:
WebProNews SearchNewz
WebProWorld DevWebPro
Marketing SecurityNews
Plus over 100 other newsletters!

Send me relevant info on products and services.

Ten most recent posts.

Featured Software

WebProNews in the News
View all recent mentions of WebProNews from around the world!

Recent Articles On ...
Google eBusiness
Yahoo Ask Jeeves
MSN Blogs
Search Engines Blogging
Affiliate Programs Marketing
eCommerce Advertising
eBay Sun Microsystems
AOL Adsense
Microsoft Adwords
Oracle IBM
Amazon Apple
SEO iPod
Adsense XBox
PR Adobe

iEntry.com WebProWorld RSS Feed WebProWorld Contact WebProNews Print Version Email a friend Bookmark us