It works for me. You didn't provide a test case, so I had to write my own:

my @a = ( [4129290, 1675967, 2412031, '41%', '/usr/bin' ], [4129290, 1675967, 2412031, '41%', '/usr' ], [4129290, 1675967, 2412031, '41%', '/local' ], ); sorter(@a); __END__ output ====== before sort: /local Sorted by drive: /usr/bin

btw, you should use my in front of @beforeSort and @sorted_by_drive, and you'd probably be better off (due to smaller memory utilisation) to accept an array reference as an argument instead of an array, and returning an array reference, instead of the array.

Maybe you're not preparing the array properly? Here's an example:

my @df_k = split(/\n/, `df -k`); shift(@df_k); # Remove header. my @sorted_by_drive = sort { $a->[5] cmp $b->[5] } map { [ split(/\s+/, $_, 6) ] } @df_k; use Data::Dumper (); print(Data::Dumper->Dump( [ \@sorted_by_drive ], [qw( $sorted_by_drive )], ));

Updated reply to updated question:

1) Please please please please use use strict! You're not good enough to not use it yet.

2)

sub parseIntoArraySolaris (@df) ^^ what does this mean? It compiles, surprisingly, but it's not listed in docs and it doesn't give the args to the var @df.

3) Hold on, isn't [4] suppose to be a string? Why are you dividing a string by 1024? Similarly, [3] is a percentage, not an amount.

4) The function could use "some" improvement:

sub parseIntoArraySolaris { my @rows; my $element; foreach $element (@_) { my ($a, $b, $c, $d, $e, $f) = split(/\s+/, $element); push(@rows, [ $b/1024, $c/1024, $d/1024, $e, $f ]); } return @rows; }

which can be rewritten as:

sub parseIntoArraySolaris { return map { my ($a, $b, $c, $d, $e, $f) = split(/\s+/, $_); [ $b/1024, $c/1024, $d/1024, $e, $f ] } @_; }

and here's how it's used:

my @df_k = split(/\n/, `df -k`); shift(@df_k); # Remove header. my @beforeSort = parseIntoArraySolaris(@df_k); my @sorted_by_drive = sort { $a->[4] cmp $b->[4] } @beforeSort; use Data::Dumper (); print(Data::Dumper->Dump( [ \@sorted_by_drive ], [qw( $sorted_by_drive )], ));

wich can be rewritten as:

# Get data. my @df_k = split(/\n/, `df -k`); # Remove header. shift(@df_k); # Parse line. foreach (@df_k) { my ($a, $b, $c, $d, $e, $f) = split(/\s+/, $_); $_ = [ $b/1024, $c/1024, $d/1024, $e, $f ]; } # Sort. my @sorted_by_drive = sort { $a->[4] cmp $b->[4] } @df_k; # Quick way of printing for now. use Data::Dumper (); print(Data::Dumper->Dump( [ \@sorted_by_drive ], [qw( $sorted_by_drive )], ));

In reply to Re: How do I sort a Multidimensional array (not Hash)? by ikegami
in thread How do I sort a Multidimensional array (not Hash)? by auyong

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.