Re: sort filename array the same way as windows explorer



On Feb 1, 2:03 pm, "Paul Lalli" <mri...@xxxxxxxxx> wrote:
On Feb 1, 1:04 pm, panof...@xxxxxxxxx wrote:



I've searched and searched... I've seen references to sort:naturally
and other, but I do not have enough experience to write a piece of
code that will sort an array the same way that windows explorer sorts
its file list.

Here is my list I want sorted:

AsM-00 (EPC) 20070125 173425.wmf
AsM-01 (EPC) 20070125 173425.wmf
AsM-01-01 (EPC) 20070125 173425.wmf
AsM-01-02 (EPC) 20070125 173425.wmf
AsM-01-03 (EPC) 20070125 173425.wmf
AsM-01-04 (EPC) 20070125 173425.wmf
AsM-01-05 (EPC) 20070125 173425.wmf
AsM-01-06 (EPC) 20070125 173425.wmf
AsM-02 (EPC) 20070125 173425.wmf
CpM-00 (EPC) 20070125 173425.wmf
CpM-01 (EPC) 20070125 173425.wmf
CpM-01-01 (EPC) 20070125 173425.wmf
CpM-01-02 (EPC) 20070125 173425.wmf

The desired sorted result should look the same as it would in windows
explorer like this:

AsM-00 (EPC) 20070125 173425.wmf
AsM-01-01 (EPC) 20070125 173425.wmf
AsM-01-02 (EPC) 20070125 173425.wmf
AsM-01-03 (EPC) 20070125 173425.wmf
AsM-01-04 (EPC) 20070125 173425.wmf
AsM-01-05 (EPC) 20070125 173425.wmf
AsM-01-06 (EPC) 20070125 173425.wmf
AsM-01 (EPC) 20070125 173425.wmf
AsM-02 (EPC) 20070125 173425.wmf
CpM-00 (EPC) 20070125 173425.wmf
CpM-01-01 (EPC) 20070125 173425.wmf
CpM-01-02 (EPC) 20070125 173425.wmf
CpM-01 (EPC) 20070125 173425.wmf

The difference is subtle.
I hope some one can provide a robust, and elegant piece of source code
that can do this sort.
That would be very appreciated.

I don't know for a fact that this is true, but it *appears* that
Windows is sorting the filenames as they would be sorted if the non-
alphanumeric characters were not present. If that's true, the
following does the job:

@sorted = map { $_->[0] }
sort { $a->[1] cmp $b->[1] }
map { [ $_, do { $t = $_; $t =~ s/[^a-zA-Z0-9]//g;
$t } ] } @files

We're using a Schwartzian Transform to generate a list of all the
files that have the non-alphanumerics removed, and have them
correspond to their originals. Then we sort on the modified version,
and return back the originals.

For more information:
perldoc -f map
perldoc -f sort

Hope this helps,
Paul Lalli


Well Mr. Lalli ... Mr. Lilly says THANK YOU! That worked! It was just
missing the ending semicolon.
VERY MUCH APPRECIATED.

.



Relevant Pages

  • Re: sort filename array the same way as windows explorer
    ... The desired sorted result should look the same as it would in windows ... that can do this sort. ... correspond to their originals. ... perldoc -f sort ...
    (comp.lang.perl.misc)
  • sort filename array the same way as windows explorer
    ... I've seen references to sort:naturally ... code that will sort an array the same way that windows explorer sorts ... I hope some one can provide a robust, and elegant piece of source code ... that can do this sort. ...
    (comp.lang.perl.misc)
  • Re: order a semicolon-separated data file by a value of a column
    ... >>My suggestion would be to read each element, store each line in an array ... >>reference, store each reference in a larger array, sort the array by the ... type 'perldoc ... > references, I read the lama book and it does't mention references. ...
    (comp.lang.perl.misc)
  • Re: Comparing numbers
    ... Perl's 'sort' function is quite flexible, and supports numerically sorting lists. ... There is also a nice FAQ entry that can be found with `perldoc -q sort`. ... `perldoc -q keyword` searches the perl FAQ for keywords, ...
    (perl.beginners)
  • Re: help on hasah usage
    ... I am new to hash usage in perl and wanted to learn more on how to use ... Read each line of the file (perldoc -f readline, ... obtain a list of keys of the hash ... sort those keys ...
    (perl.beginners)