Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The default sort order is lexical. If you want a numeric sort, you need to specify a sort routine using the <=> operator (see perlop). See sort for more information.

Since your filenames have both numeric and alpha components, you will probably need to split the pieces apart before doing the comparison. The Schwartzian Transform is good for this kind of thing.

Here is one approach. Extend the sort routine as needed if you want to specify a secondary/etc sort order in the event two filenames start with the same numeric component.

use strict; use warnings; my @files = qw( 9.10.force.0.5.1LGY.pdb 29.30.force.0.5.1LGY.pdb 30.31.force.0.5.1LGY.pdb 31.32.force.0.5.1LGY.pdb 100.101.force.0.5.1LGY.pdb 101.102.force.0.5.1LGY.pdb ); my @sorted = map { $_->[0] } sort { $a->[1] <=> $b->[1] } map { [ $_, split( /\./, $_ ) ] } @files; print "$_\n" for @sorted;

Love those pdb files. :-) Lipase, huh? Interesting stuff.


In reply to Re: sorting an array of file names by bobf
in thread sorting an array of file names by hotel

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?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-20 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found