Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Quick sort question..... 3

Status
Not open for further replies.

JimJx

Technical User
Feb 16, 2001
202
US
Hi all,

I am using @list = sort @unsortlist; to do a quick sort. However, I am not completely happy with the output.....

Since this seems to be a straight ASCII sort, I get something similar to:

B, C, D, a, b, c

What I would like to get is something like this:
a, B, b, C, c

Anyone have a suggestion or routine handy? :)

Thanks in advance,
Jim
 
This will work:
Code:
@list = sort { uc($a) cmp uc($b); } @unsortlist;
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Very cool....

Learn something new everyday (or at least try to since I am so new with PERL :) )

Thanks Tracy
 
Quick note, the above sort routine may mix the order of each upper/lower case pair. For instance:
Code:
# Using these arrays:
@sort1 = qw/ a B c D e A b C d E /;
@sort2 = qw/ a b c d e A B C D E /;
# this sort:
print sort{uc($a) cmp uc($b)}@sort1,"\n";
print sort{uc($a) cmp uc($b)}@sort2,"\n";
# results in:
aABbcCdDeE
aAbBcCDdeE
However, with the concatenation operator, this is easy to resolve. eg:
Code:
# But this sort:
print sort{uc($a).$a cmp uc($b).$b}@sort1,"\n";
print sort{uc($a).$a cmp uc($b).$b}@sort2,"\n";
# results in:
AaBbCcDdEe
AaBbCcDdEe

Hope this helps,

brendanc@icehouse.net
 
Nice Brendan :) Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Very nice Brendan, Thank you.

Jim
 
You get a star for that one Brendan! Very clever! I realized when I posted that solution that it would mix cases like you mentioned, but couldn't come up with a quick way around it. I really like your solution! Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top