in reply to Re^3: Sorting based on any column
in thread Sorting based on any column
It depends on whether your sample data represents an array-of-arrays, with each non-whitespace token as an element of a sub-array, or a single-level array with each line as an element. In the first case, it's fairly simple, something like this:
sub sort_aoa { my( $array, $column ) = @_; return sort { $a->[$column] cmp $b->[$column] } @$array; }
If each element is a whole line, you'll have to split them into words before sorting. This is where a Schwartzian Transform is likely to help the most, but I'll show the basic idea and you can add that:
sub sort_lines_by_column { my( $array, $column ) = @_; return sort { return( (split ' ', $a)[$column] cmp (split ' ', $b)[$column] ); } @$array; }
(Untested. In both cases, replace the 'cmp' comparison with whatever you want.)
Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Sorting based on any column
by Anonymous Monk on May 21, 2015 at 12:46 UTC | |
by aaron_baugher (Curate) on May 21, 2015 at 13:39 UTC | |
by AnomalousMonk (Archbishop) on May 21, 2015 at 15:08 UTC | |
by aaron_baugher (Curate) on May 21, 2015 at 21:46 UTC | |
by aaron_baugher (Curate) on May 21, 2015 at 13:28 UTC | |
by AnomalousMonk (Archbishop) on May 21, 2015 at 14:27 UTC | |
by dasgar (Priest) on May 21, 2015 at 18:50 UTC |