NewtonianB
asked on
sorting one array, matching the second one
I have two arrays of strings, the left one needs to be natural sorted, I can do this however array2 has to be modified to maintain the index consistency.
@array1 @array2
1 --> b 1 --> bla
2 --> d 2 --> bla2
3 --> a1 3 --> bla3
4 --> a10 4 --> bla4
5 --> a20 5 --> bla5
6 --> a2 6 --> bla6
1 --> a1 1 --> bla3
2 --> a2 2 --> bla6
3 --> a10 3 --> bla4
4 --> a20 4 --> bla5
5 --> b 5 --> bla
6 --> d 6 --> bla2
i'm using a modified version of tye algorithm found on perkmonkey site for the natsort on array1 but then how would I order array2.
my @sorted = @list[
map { unpack "N", substr($_,-4) }
sort
map {
my $key = $list[$_];
$key =~ s[(\d+)][ pack "N", $1 ]ge;
$key . pack "N", $_
} 0..$#list
];
Would you use a two dimensional array instead?
I want to make sure i'm doing this the most efficient way as the list can get long, any help appreciated
@array1 @array2
1 --> b 1 --> bla
2 --> d 2 --> bla2
3 --> a1 3 --> bla3
4 --> a10 4 --> bla4
5 --> a20 5 --> bla5
6 --> a2 6 --> bla6
1 --> a1 1 --> bla3
2 --> a2 2 --> bla6
3 --> a10 3 --> bla4
4 --> a20 4 --> bla5
5 --> b 5 --> bla
6 --> d 6 --> bla2
i'm using a modified version of tye algorithm found on perkmonkey site for the natsort on array1 but then how would I order array2.
my @sorted = @list[
map { unpack "N", substr($_,-4) }
sort
map {
my $key = $list[$_];
$key =~ s[(\d+)][ pack "N", $1 ]ge;
$key . pack "N", $_
} 0..$#list
];
Would you use a two dimensional array instead?
I want to make sure i'm doing this the most efficient way as the list can get long, any help appreciated
ASKER
Thanks Adam! but It needs to be natural sorted instead of sort which is what's creating my complication, how would I use the above natural sorting algorithm or another one with your code
example of proper sort: a1 b1 a10 b1 b20 instead of a1 a10 b1 b20
example of proper sort: a1 b1 a10 b1 b20 instead of a1 a10 b1 b20
ASKER
i meant for example a1 a2 a3 a10 a12 instead of a1 a10 a12 a2 a3
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
that should work! thanks!
Last quick question,
What's the most efficient way to get a 2Dimensional array consisting of the last two columns of array AoA_sorted?
could i just do something like
my @arrayTwoDim = map{$_->[1], $_->[2]} @AoA_sorted;
Last quick question,
What's the most efficient way to get a 2Dimensional array consisting of the last two columns of array AoA_sorted?
could i just do something like
my @arrayTwoDim = map{$_->[1], $_->[2]} @AoA_sorted;
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I could not have asked for a better help. Thank you so much!
Open in new window