Minus some misformatted delimiters on your my @item_array = ... line in the OP, everything you've posted seems good. Specifically,

#!/usr/bin/perl use strict; use warnings; my @item_array = ([1, 2, 20], [2, 3, 15], [3,4,3], [4, 5, 17], [5,6,28], [6,1,23], [1, 7, 1], [2, 7, 4], [3, 7, 9,], [4, 7, 16], [5, 7, 25], [6, 7, 36], ); my @sorted_array = sort { $a->[2] <=> $b->[2] } @item_array; for (@sorted_array) { print join("\t", @$_), "\n"; }

Outputs:

1 7 1 3 4 3 2 7 4 3 7 9 2 3 15 4 7 16 4 5 17 1 2 20 6 1 23 5 7 25 5 6 28 6 7 36

Note I copied/pasted the sort. As well, note that curly brackets are used in hash references, square brackets in array references, and parentheses in list construction. See perlreftut.

Are you sure that your array contains what you think it contains? See How can I visualize my complex data structure?. The short answer is is use Data::Dumper to make sure your array contains what you think it does: use Data::Dumper; print Dumper \@item_array;


In reply to Re: Sort multidimensional array by third item by kennethk
in thread Sort multidimensional array by third item by rosalindwills

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.