Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

If your read perldoc -f sort, you should learn all that you need to know. If you have 5.8 (?) or later, you can also read about the sort pragma with perldoc sort.

I'm not sure how you are defining an alphanumeric sort (do you want digits before or after alphas?), so for the purpose of simplicity, I'll assume that you want a case-insensitive sort so that you don't have to worry worry about 'a' coming after 'Z'. To do this, you provide a sort subroutine. There are various ways to do this.

@new = sort { lc $a cmp lc $b } @old;

Some people prefer not to use an anonymous subroutine, so you have the option of naming it.

# incidentally, why doesn't this trigger a bareword # warning under strict? @new = sort alphanumeric @old; sub alphanumeric { lc $a cmp lc $b }

Again, read through the docs for more information on this.

As a small side note, be aware that providing a block or subroutine to the sort function will automatically slow down its performance as is will no longer be relying on straight C code to perform the sort. Naturally, I rarely feel it's necessary to consider this issue, but it's nice to know.

Cheers,
Ovid

Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.


In reply to Re: Changing Perl's sort Default by Ovid
in thread Changing Perl's sort Default by @rocks

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2022-12-05 11:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?