Re: sort filename array the same way as windows explorer
- From: panofish@xxxxxxxxx
- Date: 1 Feb 2007 13:40:40 -0800
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.
.
- References:
- sort filename array the same way as windows explorer
- From: panofish
- Re: sort filename array the same way as windows explorer
- From: Paul Lalli
- sort filename array the same way as windows explorer
- Prev by Date: Re: Upload file fails
- Next by Date: Re: Randomly Choose from an Array
- Previous by thread: Re: sort filename array the same way as windows explorer
- Next by thread: Storing results in array rather than printing
- Index(es):
Relevant Pages
|