Trying to create a Subroutine to sort array based on any column numerically when my array is having both characters and numercis Below code doesnt seem to work
#!/usr/bin/perl $objd=sprintf "%10.0f %10.0f %10.0f %10.0f %10.0f", "15","4","5","6"," +7"; $objd2=sprintf "%10.0f %10.0f %10.0f %10.0f %10.0f", "8","9","10","11" +,"12"; push @numbers,$objd; push @numbers,$objd2; sub sort_array { @unsorted_array = @{$_[0]}; $col_2sort = $_[1]; ## Implementing Schwartzian Transform algorithm to sort in fas +test way @sorted_array= map { # Get original line back $_->[0] } sort { # Compare input fields $b->[$col_2sort] <=> $a->[$col_2sort] } map { # Turn each line into [original line, input +field] [ $_, (split " ", $_)[$col_2sort] ] } @unsorted_array; } printf "UN-Sorted Array\n"; foreach my $line ( @numbers ) { printf "$line\n"; } @sorted_array= sort_array(\@numbers,2); printf "\n\n\n"; printf "Sorrted Array\n"; foreach my $line ( @sorted_array ) { printf "$line\n"; }
In reply to Sorting based on any column by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |