Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

How to sort in this upper / lower case?

by gube (Parson)
on Dec 26, 2005 at 12:39 UTC ( [id://519130]=perlquestion: print w/replies, xml ) Need Help??

gube has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

If i sort the array text after sort i want the output in the alphabetical order, By ignoring upper case and lower case. please help me.

@array = qw(ABU gubu abu GUBU babu BABU); @array = sort {$a cmp $b} @array; print "@array"; Getting o/p : ABU BABU GUBU abu babu gubu Expected o/p : abu ABU babu BABU gubu GUBU

Replies are listed 'Best First'.
Re: How to sort in this upper / lower case?
by Aristotle (Chancellor) on Dec 26, 2005 at 12:49 UTC

    Or, as an auxiliary answer for completeness’ sake that you should probably ignore: if the array is very large and the strings are long,

    @array = @array[ do { my @lc = map lc, @array; sort { $lc[ $a ] cmp $lc[ $b ] } 0 .. $#array; } ];

    Makeshifts last the longest.

Re: How to sort in this upper / lower case?
by tirwhan (Abbot) on Dec 26, 2005 at 12:42 UTC
    @array = qw(ABU gubu abu GUBU babu BABU); @array = sort { if (lc($a) eq lc($b)) { $b cmp $a } else { lc($a) cmp lc($b) }} @array; print "@array"; __OUTPUT__ abu ABU babu BABU gubu GUBU

    Update: Oops, sorry, missed reliable ordering of same words with different case. This version does it, taking into account that you want lower case before upper case.


    A computer is a state machine. Threads are for people who can't program state machines. -- Alan Cox
Re: How to sort in this upper / lower case?
by TedPride (Priest) on Dec 26, 2005 at 21:41 UTC
    Aristotle's solution is fastest (about 6x faster), even when modified for proper ordering, but it's not terribly intuitive. I'd personally go with slower and easier to understand:
    @array = sort { lc($a) cmp lc($b) || $b cmp $a } @array;
    This does more or less the same thing as the first solution, only neater.

      is fastest (about 6× faster)

      Actually, you can’t make such a blanket statement. It might be 2 or 3× slower if the dataset is small, like the sample dataset in the OP’s post (I didn’t bother to check). But it can also easily be 100× faster if the dataset is humongous. Results will vary by dataset size, so you can’t give numbers.

      Another aspect not to be forgotten is that it always takes twice the memory.

      Makeshifts last the longest.

Re: How to sort in this upper / lower case?
by ambrus (Abbot) on Dec 26, 2005 at 22:26 UTC

    Many locales (but not the default C locale) sort this way, so if you use locale; then cmp or sort will do what you want. For example:

    [am]king ~$ LC_COLLATE=hu_HU perl -we 'use locale; print join(" ", sor +t(qw"ABU gubu abu GUBU babu Babu bubu")), "\n";' abu ABU babu Babu bubu gubu GUBU [am]king ~$ LC_COLLATE=hu_HU perl -we 'print join(" ", sort(qw"ABU gub +u abu GUBU babu Babu bubu")), "\n";' ABU Babu GUBU abu babu bubu gubu

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://519130]
Approved by astaines
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-23 07:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found