Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3306972
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:21:06+00:00 2026-05-17T21:21:06+00:00

I have a data structure like the following @colors = qw(red blond green); @numbers

  • 0

I have a data structure like the following

@colors = qw(red blond green);
@numbers = qw(349 1234.5678 3.14159265);
@hats = qw(fedora porkpie bowler);
my %hash = (colors => \@colors, numbers => \@numbers, hats => \@hats);

I want to sort this according to the values of one of the arrays, maintaining the association of parallel array elements. That is, if I exchange $hash{numbers}[2] and index $hash{numbers}[3], i want to do the same exchange for all the other arrays in the hash. In this case, if I sort {$a <=> $b} on numbers:

$sorted{numbers} = [3.14159265, 349, 1234.5678];
$sorted{colors}  = ["green", "red", "blond"];
$sorted{hats}  = ["bowler", "fedora", "porkpie"];

The solution i’m using now inverts the structure of %hash into an array where $array[$i]{$k} == $hash{$k}[$i], does @sorted = sort {$a->{numbers} <=> $b->{numbers}} @array, then converts @sorted back from array of hashes to hash of arrays.

I don’t really care if the sort is stable, I just wonder if there’s a better way to do it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T21:21:07+00:00Added an answer on May 17, 2026 at 9:21 pm

    Here’s a trick I’ve used.

    my @permutation = sort { $numbers[$a] <=> $numbers[$b] } (0..$#numbers);
    @colors = @colors[@permutation];
    @numbers = @numbers[@permutation];
    @hats = @hats[@permutation];
    # No change to %hash needed, since it has references to above arrays.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JavaScript data structure like the following in my Node.js/Express web app:
I have something like the following data structure: Category StartDateTime EndDateTime =============================================== 1 12/1/2009
I have a Core Data model structure like the following: Product <-->> OrderProduct where
I have an XML structure like the following: <tables> <table name=tableName1> <row ID=34 col1=data
I have a Perl data structure like the following: %hash = ( table_id =>
Let's say I have a data structure like the following: Camera { double x,
I am constructing enum values that have a data structure like the following: enum
My table have data structure like this cate_id task_id date_start date_end other 34 14
Using EF4 and Linq I have a data structure which looks like this: parentid
I have a JSON-like data structure (which I don't want to change) that is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.