I'm trying to sort arrays within an array. For eg. say
@array = (['CCI003', '1', 'M'], ['CCI002', '1', 'N'], ['CCI001', '1', 'U'], ['CCI002', '2', 'N'])
then after sorting the array would look like
(['CCI001', '1', 'U'], ['CCI002', '1', 'N'], ['CCI002', '2', 'N'], ['CCI003', '1', 'M'])
The sorting being done on the FIRST element within each sub-array. Here is my test code:
#!/usr/perl5/bin use strict; my @array; my $intElements; my @prefix1; my @prefix2; my @prefix3; my @prefix4; my @sorted; @prefix1 = ('CCI003', '1', 'M'); @prefix2 = ('CCI002', '1', 'N'); @prefix3 = ('CCI001', '1', 'U'); @prefix4 = ('CCI002', '2', 'N'); print STDOUT "Individual arrays: \n"; print STDOUT "@prefix1 \n"; print STDOUT "@prefix2 \n"; print STDOUT "@prefix3 \n"; print STDOUT "@prefix4 \n"; $intElements = push @array, \@prefix1; $intElements = push @array, \@prefix2; $intElements = push @array, \@prefix3; $intElements = push @array, \@prefix4; print STDOUT "Final Array: \n"; foreach my $rec (@array) { print STDOUT "$rec->[0], $rec->[1], $rec->[2] \n"; } @sorted = sort {$$a[0] <=> $$b[0]} @array; print "\n"; print STDOUT "Sorted Array: \n"; foreach my $rec (@sorted) { print STDOUT "$rec->[0], $rec->[1], $rec->[2] \n"; }
It dosen't seem to do anything to the sort order. This is the first time I'm working with array sorting.....so I might be missing something pretty stupid. Any help is appreciated.

In reply to 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.