For instance, with this:

print for map { substr $_, 30 } sort map { sprintf "%-30s%s", (split / /, (split /\|/)[1])[1], $_ } <DATA>; __DATA__ 123|Pete Smith|4321|321G4 2134|Mike Malarky|93F821 83|Dave Waylaid|8374W

Output:

2134|Mike Malarky|93F821 123|Pete Smith|4321|321G4 83|Dave Waylaid|8374W

The difficult part is that nested split. The inner one, (split /\|/)[1] selects the second field in your data. Then the result is fed to the outer, (split / /, inner)[1], which extracts the second word. That is mapped to a string like this: Malarky                       2134|Mike Malarky|93F821. This string is ASCIIbetically sorted and then, the right part of it is extracted with substr and returned to print. This technique (mapping to a string, sorting and extracting the previously mangled data) is called Guttman-Rosler transform, google for it.

As a side note, I'd like to point out that the first line in your example data appears to have four fields separated with a pipe, while the rest have only three fields.

--
David Serrano


In reply to Re: custom sorting by Hue-Bond
in thread custom sorting by Anonymous Monk

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.