Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting one array according to another

Status
Not open for further replies.

Tve

Programmer
May 22, 2000
166
0
0
FR
Hi,
I'm trying to sort on array containing strings according to one containning values, eg:

$string[0] = "a";
$string[1] = "d";
$string[2] = "c";
$string[3] = "b";

$value[0] = 111;
$value[1] = 100;
$value[2] = 150;
$value[3] = 150;

So, I need an array that would return

$return[0] = "d";
$return[1] = "a";
$return[2] = "b";
$return[3] = "c";

I've tried using hashes, but since I sometimes get the same values (150 twice), my hash does not contain the same amount of entries as my $string array, eg:

$return{100}="d";
$return{111}="a";
$return{150}="b";

How can I resolve this?

Regards,

Thierry




 
This one sounds fun...
So, to double-check your intention, you need to sort one array based on the values corresponding to the same elements in another array.
One possibility is to make an array, each entry being a reference to an array containing two elements, the corresponding element from each of the two arrays you're using. then sort based on only one of the entries sort of a swartzian transform, but only after you combined the two arrays into one. You can do this anytime you need a structure of two elements to a spot, but with duplications allowable.
Which actually leads to a question i have: can you get the index of a current element using a map? I'm not sure, but i have a way around it, assuming map starts at the first element of a list (which i'm pretty sure it does). Here's my idea coded, but untested:
[tt]
my @to_be_sorted;
my @sort_indexes;
my @sorted;
@sorted =
map {$_->[0]} (
sort {$a->[1] <=> $b->[1]} (
map {[$_, shift(@sort_indexes)]} @to_be_sorted));
[/tt]

This may not be sytactically perfect, but you should get the idea.


Another way to do it would be to take the array with the correct sorting values (@sort_indexes), then manipulate it as follows:
[tt]
for (my $i; $i <= length(@sort_indexes); $i++) {
$sort_indexes[$i] = [$sort_indexes[$i], $i];
}
[/tt]

Then do the sort on that, then map it to return the sorted list:
[tt]
@sorted =
map {$to_be_sorted[$_->[1]]} (
sort {$a->[0] <=> $b->[0]} @sort_indexes);
[/tt]

Only problem with both of my approaches is that they alter the @sort_indexes (or delete it). you could alter it so that it wouldn't by creating a temporary array. I'll spend more time thinking about this, and i might be able to come up with a better (or at least different) solution.
Hope this works. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top