Here is a reply to an email on another list regarding sorting of
array of arrays.  Cut and run(I did it on AS 5.6.0 build 623).  From this
you should be able to do what you want.

Wags ;)

============================================================================
=====================

Philip Newton [[EMAIL PROTECTED]] wrote:

> How to sort multidimentional arrays?can anybody help me

Well Perl doesn't really have multidimensional arrays. I assume you mean
arrays of references. And how to do it depends on what you want to do. Here
are two examples (tested):

Cut below
#!perl -w

use strict;
use diagnostics;

sub by_first() {
    $a->[0] <=> $b->[0];
}

sub numerically() {
    $a <=> $b
}

my @array = (
    [ 117, 148,  20,  21, 187 ],
    [ 126,  61, 186, 124,  39 ],
    [  86, 238,  31,  28, 203 ],
    [ 253, 250, 255, 155, 218 ],
    [   9, 154, 134, 139, 119 ],
    [ 246, 135, 179, 203, 205 ],
    [ 248, 134, 227, 103,  37 ],
    [ 174,  52, 158, 234,  61 ]
);

my @a2 = sort by_first @array;

my @a3 = sort by_first map { [ sort numerically @$_ ] } @array;

my $row;

print "original:\n";
foreach $row (@array) {
    print "Row: ", sprintf("%3d " x 5, @$row), "\n";
}

print "\n";
print "a2:\n";
foreach $row (@a2) {
    print "Row: ", sprintf("%3d " x 5, @$row), "\n";
}

print "\n";
print "a3:\n";
foreach $row (@a3) {
    print "Row: ", sprintf("%3d " x 5, @$row), "\n";
}

^---Cut to here

@array contains some random integers (courtesy of Fourmilab's Hotbits
server), which I then sort in two different ways. @a2 just sorts each row by
the first elements of the row, while @a3 sorts each row numerically and then
sorts the resulting array by the first element. Note that the sorting
routine for @a3 requires a two-pass approach: first I sort each row (inside
the map) and then I pass the sorted rows to the outside sort. For a
three-dimensional array, you'd need three levels of sorting.

Sorting by columns rather than rows is left as an exercise for the reader.

Cheers,
Philip

---
You are currently subscribed to perl-win32-users as:
[[EMAIL PROTECTED]]
To unsubscribe, forward this message to
         [EMAIL PROTECTED]
For non-automated Mailing List support, send email to  
         [EMAIL PROTECTED]

-----Original Message-----
From: Harry Goldhagen [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, July 24, 2001 07:41
To: [EMAIL PROTECTED]
Subject: Sort an array of arrays?


Anyone know how to sort an array of arrays? To sort on the first column ($j 
= 0), I tried:

$j = 0;
for $i (0..$#AddBook) {
@col = $AddBook[$i][$j];
@sortcol = sort @col;
print "Column $j contains: @col\n";
}

for $i (0..$#AddBook) {
print "Column $j sorted contains: @sortcol\n";
}

I get the last value repeated in each entry of @sortcol

Harry Goldhagen
[EMAIL PROTECTED]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to