# How to Sort on a substr

• July 15th, 08:54 PM
Jeff
How to Sort on a substr
Hi,

I need to do a sort on a couple of column ranges.

I want to be able to do a primary sort, on say, columns 21-25 and
then a secondary sort on columns 40-49.

Any ideas on how to approach this?

TIA

/j

__________________________________
Do you Yahoo!?
Yahoo! Mail - You care about security. So do we.
http://promotions./new_mail
• July 15th, 09:02 PM
Gunnar
Re: How to Sort on a substr
Jeff Westman wrote:

Yes.

1. Check out the docs for the sort() function.

2. Check the Perl FAQ:

perldoc -q "How do I sort an array by (anything)?"

--
Email: http://www.gunnar.cc/cgi-bin/contact.pl
• July 15th, 09:35 PM
Rob
Re: How to Sort on a substr
Jeff Westman wrote:

Can you get away with concatenating the 2 substrings? That would cut
down your sort to pass. Either way you'll have to define a sort
function I think.

• July 16th, 01:19 AM
John
Re: How to Sort on a substr
On Thursday 15 July 2004 13:54, Jeff Westman wrote:

Hello,

This should work:

my sorted = map substr( \$_, 15 ),
sort
map substr( \$_, 20, 5 ) . substr( \$_, 39, 10 ) . \$_,
unsorted;

John
--
use Perl;
program
fulfillment

• July 16th, 12:13 PM
Bob
RE: How to Sort on a substr
Jeff Westman wrote:

Assuming you're numbering columns from 1:

rows = sort { substr(\$a, 20, 5) cmp substr(\$b, 20, 5)
|| substr(\$a, 49, 10) cmp substr(\$b, 49, 10) } rows;

If you're numbering columns from 0, adjust the offsets above.

The general concept for multi key sorting is to combine the comparisons with
a logical "or" operation.

John Krahn gave you a sort of modified "Schwartzian Transform" (did I spell
that right?) approach which is faster if you have a large number of rows to
process, but a bit less clear...