If your set of prefixes if reasonably small, but the format and/or the variations in format make it difficult to map the sort order cleanly, then another possibility is to use a lookup table for your sorting. That way you pre-specify the order you want them in and let sort use that pre-specified order to do its work.

This isn't a very convincing demonstration, but it should serve as an illustration. It's greatest asset is its flexibility, though on large datasets it should also be pretty efficient.

#! Perl -sw use strict; my $index = 0; my %lookup = map { $_ => $index++; } qw( CCI001 CCI002 CCI002A CCI002B CCI100 CCI101 CCI500 CDI001 CDJ001 ); my @data = ( [ qw(CDJ001 1 N)], [ qw(CCI100 1 M)], [ qw(CCI001 1 M)], [ qw(CCI002B 1 N)], [ qw(CCI002 1 N)], [ qw(CDI001 1 M)], [ qw(CCI500 1 M)], [ qw(CCI002A 1 N)], [ qw(CCI101 1 N)], ); my @sorted = sort{ $lookup{$$a[0]} <=> $lookup{$$b[0]} } @data; print "@{$_}\n" for @sorted; __DATA__ # Output C:\test>196991 CCI001 1 M CCI002 1 N CCI002A 1 N CCI002B 1 N CCI100 1 M CCI101 1 N CCI500 1 M CDI001 1 M CDJ001 1 N C:\test>

Well It's better than the Abottoire, but Yorkshire!

In reply to Re: Sorting multi-dimensional arrays by BrowserUk
in thread Sorting multi-dimensional arrays by cryptic

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.