Search code examples
perlarrayssortingnumeric

Sort Perl numeric array


I have an array which looks like this:

array[0]: 6 8
array[1]: 12 9 6
array[2]: 33 32 5
array[3]: 8 6

I want to sort this array so that it looks like this:

array[0]: 6 8
array[1]: 6 9 12
array[2]: 5 32 33
array[3]: 6 8

I know I can sort the array with @newarray = sort {$a cmp $b} @array;, but I need to sort the elements in each line as well. How can I do that?


Solution

  • Update 2021-03-16: Dear future visitors, I have the accepted answer, and I'm still getting upvotes, but @FMc's solution is clearly better than mine. (FMc and dalton have the same answer, but as far as I can tell, FMc got there first.)

    In any case, if you have a situation like this use Perl's built-in map rather than my original answer of a custom subroutine.

    Original answer: Assuming that you have an array of strings there, and that all you want is to sort each string as if it were a sub-array of numbers (but leave it a string?):

    #!/usr/bin/env perl
    use strict;
    use warnings;
    
    my @array = ('6 8', '12 9 6', '33 32 5', '8 6');
    
    foreach my $string (@array) {
        $string = split_sort($string);
    }
    
    sub split_sort {
        my $string = shift @_;
        my @internal_nums = split ' ', $string;
        @internal_nums = sort {$a <=> $b} @internal_nums;
        return join ' ', @internal_nums;
    }
    
    print "@array\n";