Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Sorting one array according to another

Sorting one array according to another

Sorting one array according to another

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:


How can I resolve this?



RE: Sorting one array according to another

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:

my @to_be_sorted;
my @sort_indexes;
my @sorted;
@sorted =
   map  {$_->[0]} (
   sort {$a->[1] <=> $b->[1]} (
   map {[$_, shift(@sort_indexes)]} @to_be_sorted));

  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:

for (my $i; $i <= length(@sort_indexes); $i++) {
    $sort_indexes[$i] = [$sort_indexes[$i], $i];

  Then do the sort on that, then map it to return the sorted list:

@sorted =
   map {$to_be_sorted[$_->[1]]} (
   sort {$a->[0] <=> $b->[0]} @sort_indexes);

  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.

"If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito."

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close