sort filename array the same way as windows explorer

Discussion in 'Perl Misc' started by panofish@gmail.com, Feb 1, 2007.

  1. Guest

    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.
     
    , Feb 1, 2007
    #1
    1. Advertising

  2. Mirco Wahab Guest

    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.
    >
    > AsM-00 (EPC) 20070125 173425.wmf
    > AsM-01 (EPC) 20070125 173425.wmf
    > AsM-01-01 (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
    > ..


    Looks like the explorer converts spaces to asc(255)
    before sorting strings (guessed).

    That would imply sth. like:

    ...
    sub newkey { (my $s=shift) =~ s/\x20+/\xff/; $s }
    ...
    sub explorersort { sort {newkey($a) cmp newkey($b)} @_ }

    print explorersort <DATA>

    __DATA__
    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
    ...

    if thats too slow (large lists) use sth. like:

    ...
    sub newkey { (my $s=shift) =~ s/\x20+/\xff/; $s }
    ...
    print map $_->[0],
    sort {$a->[1] cmp $b->[1]}
    map [$_, newkey($_)],
    <DATA>

    __DATA__
    AsM-00 (EPC) 20070125 173425.wmf
    AsM-01 (EPC) 20070125 173425.wmf
    ...

    Regards

    M.
     
    Mirco Wahab, Feb 1, 2007
    #2
    1. Advertising

  3. Paul Lalli Guest

    On Feb 1, 1:04 pm, 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
     
    Paul Lalli, Feb 1, 2007
    #3
  4. Guest

    On Feb 1, 2:03 pm, "Paul Lalli" <> wrote:
    > On Feb 1, 1:04 pm, 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.
     
    , Feb 1, 2007
    #4
  5. -berlin.de Guest

    Mirco Wahab <> wrote in comp.lang.perl.misc:

    > sub newkey { (my $s=shift) =~ s/\x20+/\xff/; $s }


    Only the first occurrence, or all of them? In the latter case, you'd
    need /g on the replacement, but the standard tool for that is tr///:

    tr/ /\xff/s

    Anno
     
    -berlin.de, Feb 2, 2007
    #5
  6. Mirco Wahab Guest

    -berlin.de wrote:
    > Mirco Wahab <> wrote in comp.lang.perl.misc:
    >
    >> sub newkey { (my $s=shift) =~ s/\x20+/\xff/; $s }

    >
    > Only the first occurrence, or all of them? In the latter case, you'd
    > need /g on the replacement, but the standard tool for that is tr///:
    >
    > tr/ /\xff/s


    The idea I had was to coalesce subsequent spaces
    into one 0xff and sort the resulting string,
    therefore your proposed /s would be correct.

    But! I re-checked windows sort order
    by some generated files:

    AsM-01-00\ \ \ 0(EPC)\ 20070125\ 173425.wmf
    AsM-01-00\ \ 0(EPC)\ 20070125\ 173425.wmf
    AsM-01-00\ 0(EPC)\ 20070125\ 173425.wmf

    AsM-01-00\ \ \ 1(EPC)\ 20070125\ 173425.wmf
    AsM-01-00\ \ 1(EPC)\ 20070125\ 173425.wmf
    AsM-01-00\ 1(EPC)\ 20070125\ 173425.wmf

    and windows sort order turn out to be:

    AsM-01-00 0(EPC) 20070125 173425.wmf
    AsM-01-00 1(EPC) 20070125 173425.wmf
    AsM-01-00 0(EPC) 20070125 173425.wmf
    AsM-01-00 1(EPC) 20070125 173425.wmf
    AsM-01-00 0(EPC) 20070125 173425.wmf
    AsM-01-00 1(EPC) 20070125 173425.wmf

    so its clear from the example that the
    magic windows sorter has to be like:

    ...
    sub hardenspaces { (my $s=shift) =~ tr/\x20/\xff/; $s }
    sub explorersort { sort { hardenspaces($a) cmp hardenspaces($b) } @_ }
    ...
    ...
    my @sorted_fnames = explorersort @fnames_from_dirread:
    ...

    as you said above.

    Thanks & Grüße

    Mirco
     
    Mirco Wahab, Feb 2, 2007
    #6
  7. Mirco Wahab Guest

    Mirco Wahab wrote:
    > The idea I had was to coalesce subsequent spaces
    > into one 0xff and sort the resulting string,
    > therefore your proposed /s would be correct.


    (WTF)

    must read: "your proposed /g would be correct"

    Sorry,

    M.
     
    Mirco Wahab, Feb 2, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    Extract filename from a filename typed by user

    =?Utf-8?B?Sm9l?=, Aug 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,068
    Travis Murray
    Aug 24, 2004
  2. Replies:
    2
    Views:
    837
    Patricia Shanahan
    Sep 24, 2006
  3. rkk
    Replies:
    9
    Views:
    847
    CBFalconer
    Sep 24, 2006
  4. Ed Hauptman
    Replies:
    7
    Views:
    1,097
    Ed Hauptman
    Aug 7, 2009
  5. Navin
    Replies:
    1
    Views:
    766
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page