in reply to sort array by part of the string
Here's a quick implementation off the top of my head:
my @xx = ( 'dfb|cy|nmju', 'dfb|my|jhiho', 'aaa|zz|gggg' ); my @sorted_xx = sort sorter @xx; print Dumper \@xx; print Dumper \@sorted_xx; sub sorter { my @arr_a = split /\|/, $a; my @arr_b = split /\|/, $b; while ( @arr_a ) { my $sa = shift @arr_a; my $sb = shift @arr_b; my $comparison = ( $sa cmp $sb ); return $comparison if $comparison; } return 0; } __END__ $VAR1 = [ 'dfb|cy|nmju', 'dfb|my|jhiho', 'aaa|zz|gggg' ]; $VAR1 = [ 'aaa|zz|gggg', 'dfb|cy|nmju', 'dfb|my|jhiho' ];
I note that in the example strings you provide, the field delimiters are always in the same place (i.e., the fields are fixed length). If that's the case, you should be able to compare them directly without split, etc.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: sort array by part of the string
by Anonymous Monk on Dec 01, 2007 at 15:15 UTC |