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

  • Comment on How do I sort something alphabetically?

Replies are listed 'Best First'.
Re: How do I sort something alphabetically?
by arturo (Vicar) on Nov 06, 2000 at 21:29 UTC

    The default sort behavior with strings is "ASCIIbetical," rather than alphabetical. ASCII-wise, the capital letters (as a group) come before the lowercase (as a group); thus "D" comes before "a."

    So assuming you want to sort a list of words, what you want is:

    # make it a "sort sub" sub alphabetical { # compares lower-cased versions of the strings lc($a) cmp lc($b); } # to sort having defined this sub, you do @list = sort alphabetical @list;

    Or, to avoid making a bunch of tiny little otherwise useless functions, sort also lets you use a block in place of a function:

    @list = sort { lc($a) cmp lc($b) } @list

Re: How do I sort something alphabetically?
by extremely (Priest) on Nov 07, 2000 at 06:43 UTC
    I gotta chime in here and point out that a simple sort with { lc($a) cmp lc ($b) } may not be what you want. You may wish to do a fancier sort like the second one in this example:
    #!/usr/bin/perl -w my @un = <DATA>; print "once:\n"; print sort { lc($a) cmp lc($b) } @un; print "\nagain:\n"; print sort { (lc($a) cmp lc($b)) or ($a cmp $b) } @un; __DATA__ Mark MARK mark Dave DAVE DAVEY Davey davey MarkY
Re: How do I sort something alphabetically?
by jptxs (Curate) on Nov 06, 2000 at 07:02 UTC
    use strict; my @list = qw(b d e c a f g w i l z x p o q s); print sort @list; print "\n\n"; jptxs:/home/jptxs $ perl -w alpha abcdefgilopqswxz jptxs:/home/jptxs $
    Basically, see sort - a function which will sort on any given criteria that defaults to sorting alphabetically for strings, or numerically for numbers (duh).

    Originally posted as a Categorized Answer.

Re: How do I sort something alphabetically?
by tedv (Pilgrim) on Nov 07, 2000 at 04:05 UTC
    Sort also lets you use a block in place of a function:
    @list = sort { lc($a) cmp lc($b) } @list

    So often you need to make a bunch of tiny little otherwise useless functions.

    -Ted

    Originally posted as a Categorized Answer.

Re: How do I sort something alphabetically?
by clemburg (Curate) on Nov 07, 2000 at 10:27 UTC

    If you are into advanced sorting orders you will want to look at Sort::ArbBiLex, which supports the construction of arbitrary sort orders.

Re: How do I sort somehting alphabetically?
by brainpan (Monk) on Nov 06, 2000 at 06:19 UTC
    Have you checked out sort?

    Originally posted as a Categorized Answer.

Re: How do I sort something alphabetically?
by St{)ìÑ (Initiate) on Oct 19, 2002 at 18:54 UTC
    Hey I have had the same problem with ASCIIbetical. It is a strange and unlogical method of sorting. The answer to this is to look at Learning Perl by O'REILLY, Chapter 15.  Strings and Sorting. In chapter 15 it will tell you EXACTLY how to change the default to any kind of sorting method you want, even a custom method!

    <STDIN>

    Originally posted as a Categorized Answer.