in reply to Re: Sorting array
in thread Sorting array

I have no idea if thats how it should sort , but this is how you would cache your solution (also known as schwartzian transform) to save on redundant split and s// (save CPU by using more memory)

#!/usr/bin/perl -- use strict; use warnings; use Data::Dumper; my @unsorted = split /[\r\n]+/, <<'__THE_DATA__'; ID12-ABC-5.1 ID9-ABC-5.1 ID3-ABC-6.1 ABC-5.1 ID12-ABC-5.1.5 ID15-ABC-6.1 ABC-6.1 ID5-ABC-5.1 ID5-ABC-5.1.5 ABC-5.1.5 __THE_DATA__ print Dumper( \@unsorted ); my @sorted = sort { @{[(split('-',$b))]}[0] =~ s/\d+// cmp @{[(split('-',$a))]}[0] =~ s +/\d+// || (split('-',$b))[-1] cmp (split('-',$a))[-1] } @unsorted; print Dumper( \@sorted ); @sorted = map { $_->[0] } sort { $b->[1] cmp $a->[1] || $b->[2] cmp $a->[2] } map { my(@f) = split /-/, $_; [ $_, scalar( $f[0] =~ s/\d+// ), $f[-1], ]; } @unsorted; print Dumper( \@sorted ); __END__ $VAR1 = [ 'ID12-ABC-5.1', 'ID9-ABC-5.1', 'ID3-ABC-6.1', 'ABC-5.1', 'ID12-ABC-5.1.5', 'ID15-ABC-6.1', 'ABC-6.1', 'ID5-ABC-5.1', 'ID5-ABC-5.1.5', 'ABC-5.1.5' ]; $VAR1 = [ 'ID3-ABC-6.1', 'ID15-ABC-6.1', 'ID12-ABC-5.1.5', 'ID5-ABC-5.1.5', 'ID12-ABC-5.1', 'ID9-ABC-5.1', 'ID5-ABC-5.1', 'ABC-6.1', 'ABC-5.1.5', 'ABC-5.1' ]; $VAR1 = [ 'ID3-ABC-6.1', 'ID15-ABC-6.1', 'ID12-ABC-5.1.5', 'ID5-ABC-5.1.5', 'ID12-ABC-5.1', 'ID9-ABC-5.1', 'ID5-ABC-5.1', 'ABC-6.1', 'ABC-5.1.5', 'ABC-5.1' ];

Replies are listed 'Best First'.
Re^3: Sorting array
by Kc12349 (Monk) on Sep 23, 2011 at 03:47 UTC

    Thanks. That is interesting. I'll have to digest it a bit.

Re^3: Sorting array
by Kc12349 (Monk) on Sep 23, 2011 at 18:20 UTC

    On closer inspection, this doesn't seem to be quite a complete solution. The below section of the approach I think is expected to return the replaced value, in this case 'ID'.

    It instead returns a 1 if a replacement is made or an empty string if it is not. It happens to produce the correct sort in this case because of the narrow data set used.

    my $string = 'ID13'; say scalar( $string =~ s/\d+// );

    Does anyone else know of a way to return the correct match in place of this line? The below is a very poor solution I expect.

    my $string = 'ID13'; say join('', grep( /[A-Za-z]/, split('',$string) ) );

      It happens to produce the correct sort in this case because of the narrow data set used.

      Actually it doesn't produce a correct sort, its very close, but not correct. But it does match your sort exactly :)

      In any case, I find having more than one cmp confusing, so I usually just pad zeros where appropriate, like

      #~ @sorted = map { $_->[0] } @sorted = map { sprintf '%-20s %s', @$_ } sort { $b->[1] cmp $a->[1] } map { my $f = $_; no warnings 'uninitialized'; my( @didots ) = split /[.]/, ( /-([.\d]+)/ )[0]; my $ret = sprintf( '%01d.%01d.%01d ', @didots); ## %03d%03d%03d should be sufficiently futureproof if(my( $digits ) = /ID(\d+)-/ ){ $ret = "1 $ret".sprintf( '%03d ', $digits ) } else { $ret = "0 $ret"; } [ $f, $ret ]; } @unsorted; print Dumper( \@sorted ); __END__ $VAR1 = [ 'ID15-ABC-6.1 1 6.1.0 015 ', 'ID3-ABC-6.1 1 6.1.0 003 ', 'ID12-ABC-5.1.5 1 5.1.5 012 ', 'ID5-ABC-5.1.5 1 5.1.5 005 ', 'ID12-ABC-5.1 1 5.1.0 012 ', 'ID9-ABC-5.1 1 5.1.0 009 ', 'ID5-ABC-5.1 1 5.1.0 005 ', 'ABC-6.1 0 6.1.0 ', 'ABC-5.1.5 0 5.1.5 ', 'ABC-5.1 0 5.1.0 ' ];

      Usually pad to 3 digits is sufficient, but sometimes I need to go to 20

Re^3: Sorting array
by Kc12349 (Monk) on Sep 23, 2011 at 18:20 UTC

    Accidental node duplication. Please ignore.