Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting 2-Dimensional Array

Status
Not open for further replies.

parkers

Vendor
Oct 21, 2002
157
GB
Hi,

Is there anyway in perl to sort 2D arrays so that the integrity of all rows and elements remain intact?

e.g.

$array[0..m][0..n]
|
sort array by row 'm'

Again any help or pointers appreciated.

Thanks.
 
If anyone is interested? I've found a quite a tidy solution to a multidimensional array sort ...

Alphabetical sort on 'element' '0':

@sort = sort { $b->[0] cmp $a->[0]} @array;

or in reverse order:

@sort = reverse sort { $b->[0] cmp $a->[0]} @array;

'Sorted' array returned in @sort with array integrity intact.

substitute <=> for cmp for a numerical sort on any array element.
 
AKA 'The Schwartzian Transform'

A very handy beast for this sort of operation. I clicked in to suggest it and you'd already found it :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top