Link to home
Start Free TrialLog in
Avatar of mock5c
mock5c

asked on

get sorted column for 2D array

Need a one-liner for extracting a single column from a 2D array, provided the index.  The returned array should be sorted (numerically).

I found code on EE for sorting a single column but it returns the entire 2D array with column i sorted.

@ary = (
[4,5,8],
[2,10,5],
[8,13,7]
);

$i = 0;
@sorted = sort {$a->[$i]<=>$b->[$i]} @ary;

I just want column i sorted and returned.
ASKER CERTIFIED SOLUTION
Avatar of ps15
ps15

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mock5c
mock5c

ASKER

I didn't test every case (only the first and last columns) so I accepted ps15's answer even though the 2nd column did not sort numerically.  tone28's suggestion does sort all columns correctly.  I'm going to contact support and ask them to assign extra points and assisted answer for tone28.