Sort 2-d array using multiple columns

S

scottmf

I have an array similar to the following (the one I am actually sorting
has 25 columns and several thousand rows):

@AoA = ([3, 1, 8],
[2, 2, 6],
[2, 1, 4],
[3, 2, 3]);

and I need to sort it by the second column, then the first column so
that my output would be as follows:

@sorted = ([2, 1, 4],
[3, 1, 8],
[2, 2, 3],
[3, 2, 6]);

I can sort by one column without a problem but have not been able to
find anything on sorting with respect to both columns. Any help would
be much appreciated.
 
G

Gunnar Hjalmarsson

scottmf said:
... have not been able to find anything on sorting with respect
to both columns. ...

Where did you look then?? "perldoc -f sort" includes examples with a
second sort criterium.
 
T

Tad McClellan

scottmf said:
@AoA = ([3, 1, 8],
[2, 2, 6],
[2, 1, 4],
[3, 2, 3]);

and I need to sort it by the second column, then the first column

have not been able to
find anything on sorting with respect to both columns.


You are expected to check the Perl FAQ *before* posting to
the Perl newsgroup.

Any help would
be much appreciated.


You already have the help you need sitting on your hard disk, no
need to ask thousands of people around the world to take the
time to help you.


perldoc -q sort

How do I sort an array by (anything)?

...
If you need to sort on several fields, the following paradigm
is useful.

@sorted = sort { ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top