http://qs321.pair.com?node_id=20802

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question: (arrays)

I want to be able to sort the "double-dimension" array by one of the colums... eg. alphabeticaly by comparing items in column 2 of the array... below some sample data:
$foo[0][0] = "zenia"; $foo[0][1] = "markownikowa"; $foo[0][2] = +"10"; $foo[1][0] = "marek"; $foo[1][1] = "butalski"; $foo[1][2] = +"20"; $foo[2][0] = "teresa"; $foo[2][1] = "parufkowa"; $foo[2][2] = +"90"; $foo[3][0] = "bogumila"; $foo[3][1] = "pierdowa"; $foo[3][2] = +"40"; $foo[4][0] = "genowefa"; $foo[4][1] = "tempawa"; $foo[4][2] = +"50"; First -- let's sort these by the [][1] column... the result should be: marek brutalski 20 zenia markownikowa 10 teresa parufkowa 90 bogumila pierdawa 40 genowefa tempawa 50 sorting by the [][2] column should give us: zenia markownikowa 10 marek brutalski 20 bogumila pierdawa 40 genowefa tempawa 50 teresa parufkowa 90
The manual covers only case of hashes and uses subroutine for comparing items. But how to adopt that for comparing such an array? Thanx in advance... Joustin

Originally posted as a Categorized Question.

  • Comment on How to sort array by columns maintaining the rows? (for C style arrays)
  • Download Code

Replies are listed 'Best First'.
Re: How to sort array by columns maintaining the rows? (for C style arrays)
by Russ (Deacon) on Jul 02, 2000 at 23:19 UTC
    Since these seem to be names, let me add a quick note about sorting on two (or more) columns.
    @data = ( [qw(Bubba brutalski 20)], [qw(Bubba brutalski 10)], [qw(JoeBob brutalski 20)], [qw(Jethro brutalski 20)], [qw(Junior brutalski 20)], ); # Sorted by Name (Last, First) @ByName = sort {$a->[1] cmp $b->[1] || $a->[0] cmp $b->[0]} @data; # Sorted by Name, then score @FullSort = sort {$a->[1] cmp $b->[1] || $a->[0] cmp $b->[0] || $a->[2] <=> $b->[2]} @data;
    The comparison operators return 0 when the operands are equal. So, using the || operator, we can sort first by Last Name, then First Name, then Score.

    In the second example above, @FullSort looks like:

    ( [Bubba, brutalski, 10], [Bubba, brutalski, 20], [Jethro, brutalski, 20], [JoeBob, brutalski, 20], [Junior, brutalski, 20] )
    because it is sorted by Last Name, then First Name, then Score.

    Russ

Re: How to sort array by columns maintaining the rows? (for C style arrays)
by Anonymous Monk on Jul 02, 2000 at 19:41 UTC
    the following code shows how to sort this data structure by any of its three columns.
    @data = ( [qw(marek brutalski 20)], [qw(zenia markownikowa 10)], [qw(teresa parufkowa 90)], [qw(bogumila pierdawa 40)], [qw(genowefa tempawa 50)], ); # Sort by first name @sorted_by_first = sort { $a->[0] cmp $b->[0] } @data; # Sort by last name @sorted_by_last = sort { $a->[1] cmp $b->[1] } @data; # Sort by score @sorted_by_score = sort { $a->[2] <=> $b->[2] } @data;
    notice that the sorts on columns 0 and 1, the names, are performed ASCIIbetically, while the sort by number is done numerically.
      First, "progressive sorting" does not work as expected: Each sort step destroys the order created by the previous sorts. You need to use the || operator as described in the above answers.

      Second, to sort your line speeds, create a hash that converts the names to sortable numbers:

      my $n=1; for (qw(115kbps 2Mbps 10Mbps T1)) {$order{$_}=$n++}
      Now, you can sort line speeds. Suppose that your list @connections contains records with the line speed in the first element (index 0). Thus,
      @result= sort {$order{$a->[0]} <=> $order{$b->[0]}} @connections;
Re: How to sort array by columns maintaining the rows? (for C style arrays)
by Anonymous Monk on Jul 14, 2000 at 18:47 UTC

    Re: How to sort array by columns maintaining the rows? (for C style arrays)

    Originally posted as a Categorized Answer.