I was given the following problem at work. A DATA sample and my solution follows the READMORE tag.

Problem: Print a directory of engineering drawings in the order determined by drawing number data contained in the filename:

 
 TYPE  Part  (Detail) Revision (ALT) layer
 1D     1              R1       B1    .vec
 18D    20     A       R0             .ras

Sort order:

  1. layer,
  2. reverse(ALT),
  3. TYPE,
  4. Part,
  5. Revision,
  6. Detail

ALT, if it exists, must be queued in reverse order (e.g. 3,2,1,'')

#!/usr/bin/perl -wl use strict; for (sort{ # TYPE: $t0_$t1_ # Part: $p0_ # Det : $pda # Rev : $r_ # ALT : $alt_ # Layer $l_ my ($t0a,$t1a,$p0a,$pda,$ra,$alta,$la) = map(m!(\d+)(\D+)(\d+)(\D*)R(\d+)B?(\d*)\.(\w+)$!,$a); my ($t0b,$t1b,$p0b,$pdb,$rb,$altb,$lb) = map(m!(\d+)(\D+)(\d+)(\D*)R(\d+)B?(\d*)\.(\w+)$!,$b); $la cmp $lb || ($altb eq ''? 0 :$altb) <=> ($alta eq '' ? 0 : $alta) || $t0a <=> $t0b || $t1a cmp $t1b || $p0a <=> $p0b || $ra cmp $rb || $pda cmp $pdb; }<DATA>){ chomp; print; } __DATA__ 18DD13AR0B2.vec 5A344R2B15.ras 5A344R2B15.vec 5A35SR0.ras 113A28R1.vec 113A28AR0.vec 113A29R0.ras 5A32R0.ras 113A29R1.vec 18AD13XR0B3.ras 18AD13XR0B3.vec 113A22R1.ras 5A35R0.vec 5A34R0.ras 113A22R1.vec 18DD12YR0B2.vec 18DD13AR0B2.ras 113A29AR0.ras 113A29AR2.vec 113A28R0.vec 5A33R0.vec 5A34R0.vec 5A35R0.ras 113A28AR2.vec 113B28R2.ras 113A28R2.vec 13B29AR0B1.ras 113B29AR0B1.vec 5A32R0.vec 5A33R0.ras

Explanation:

  • separate alpha and numeric values to sort separately,
  • compare in order of highest to lowest priority,
  • compare lower priority values only if higher order comparison returns '0'; mkmcconn

  • In reply to complex sort by mkmcconn

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.