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

  • Home
  • SEARCH
  • 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 6064089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:14:32+00:00 2026-05-23T09:14:32+00:00

I am trying to sort one array, but have a second array stay in

  • 0

I am trying to sort one array, but have a second array stay in sync with the first.

Example:

var a1 = ["human", "animal", "plant"];
var a2 = ["person", "beast", "nature"];

a1.sort();

After the sort I need to arrays to look like this:

a1 = ["animal", "human", "plant"];
a2 = ["beast", "person", "nature"];

Is there an easy way to do this, Maybe using a custom sort function?

  • 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-23T09:14:32+00:00Added an answer on May 23, 2026 at 9:14 am

    You could zip the arrays before sorting, and unzip them after sorting:

    var a = ["human", "animal", "plant"],
        b = ["person", "beast", "nature"],
        zipped = [];
    
    // zip
    for (var i=0; i<a.length; i++)
    {
        zipped.push({a: a[i], b: b[i]});
    }
    
    zipped.sort(function (x, y)
    {
        return x.a - y.a;
    });
    
    // unzip
    var z;
    for (i=0; i<zipped.length; i++)
    {
        z = zipped[i];
        a[i] = z.a;
        b[i] = z.b;
    }
    

    …but I think @duffymo’s got a better suggestion for you. Use an object/hash/associative array/map.

    var a = [{key: 'human',  value: 'person'},
             {key: 'animal', value: 'beast'},
             {key: 'plant',  value: 'nature'}];
    
    a.sort(function (x, y)
    {
        return x.key - y.key;
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to sort a list using delegates but I am getting a
Trying to do this sort of thing... WHERE username LIKE '%$str%' ...but using bound
I am trying to build a function in C/C++ to sort an array and
I'm trying to do my frist steps with jQuery but I have some trouble
I'm trying to figure out how to sort a 2d array into a 3d
I'm trying to sort column Foo that has one Bar . Currently, I'm using
Trying to sort an array by writing my own sort method using recursion (Pine's
I'm trying to get a simple sort function going using anonymous functions. One each
I am trying to sort a dict based on its key and return an
I'm looking at the MySQL docs and trying to sort out the distinction between

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.