in reply to Unusual sorting requirement

#!/usr/bin/perl # https://perlmonks.org/?node_id=1222256 use strict; use warnings; my $ary1 = [ [ 1,1,1,1,1,'2018-03-01', 'src1', 1,1,1,1 ], [ 1,1,1,1,1,'2018-02-15', 'src1', 1,1,1,1 ], [ 1,1,1,1,1,'2018-04-11', 'src1', 1,1,1,1 ], [ 1,1,1,1,1,'2018-03-11', 'src1', 1,1,1,1 ], ]; my $ary2 = [ [ 2,2,2,2,'2018-03-02', 2, 'src2', 2,2,2,2 ], [ 2,2,2,2,'2018-01-25', 2, 'src2', 2,2,2,2 ], [ 2,2,2,2,'2018-04-22', 2, 'src2', 2,2,2,2 ], [ 2,2,2,2,'2018-03-22', 2, 'src2', 2,2,2,2 ], ]; my @sorted = map $_->[0], sort { $a->[1] cmp $b->[1] } map( [ $_, $_->[5] ], @$ary1 ), map( [ $_, $_->[4] ], @$ary2 ); use Data::Dump 'dd'; dd \@sorted;

Outputs

[ [2, 2, 2, 2, "2018-01-25", 2, "src2", 2, 2, 2, 2], [1, 1, 1, 1, 1, "2018-02-15", "src1", 1, 1, 1, 1], [1, 1, 1, 1, 1, "2018-03-01", "src1", 1, 1, 1, 1], [2, 2, 2, 2, "2018-03-02", 2, "src2", 2, 2, 2, 2], [1, 1, 1, 1, 1, "2018-03-11", "src1", 1, 1, 1, 1], [2, 2, 2, 2, "2018-03-22", 2, "src2", 2, 2, 2, 2], [1, 1, 1, 1, 1, "2018-04-11", "src1", 1, 1, 1, 1], [2, 2, 2, 2, "2018-04-22", 2, "src2", 2, 2, 2, 2], ]

Replies are listed 'Best First'.
Re^2: Unusual sorting requirement
by ikegami (Patriarch) on Sep 13, 2018 at 10:56 UTC

    Here's a better optimization:

    printf "[%s]", join ",", map substr($_, 10), sort map($_->[5].$json->encode($_), @$ary1), map($_->[4].$json->encode($_), @$ary2);
Re^2: Unusual sorting requirement
by thezip (Vicar) on Sep 13, 2018 at 15:34 UTC

    Thank you for the ST!


    *My* tenacity goes to eleven...