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 9021823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:20:54+00:00 2026-06-16T05:20:54+00:00

I have two arrays. I should do a normal sort on one (descending or

  • 0

I have two arrays. I should do a normal sort on one (descending or ascending) and sort the other according to how the first array is sorted. This is because each element in the first array has a relationship with the same index element on the second array, and I must keep this relationship true. For example:

sortThis=[3,1,2];
sortAccording=["With 3","With 1","With 2];

I couldn’t find any way to take the index changes from JavaScript’s 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-06-16T05:20:55+00:00Added an answer on June 16, 2026 at 5:20 am

    Solution:
    To achieve that, you have to zip both arrays in just one. This is, given you have this two array:

    sortThis=[3,1,2];
    sortAccording=["With 3","With 1","With 2];
    

    After zip them, you will have the following array:

    zipped = [{a: 3, b: "With 3"}, {a: 1, b: "With 1"}, {a: 2, b: "With 2"}];
    

    Then, you sort it by a in order to have:

    zippedAndSorted = [{a: 1, b: "With 1"}, {a: 2, b: "With 2"}, {a: 3, b: "With 3"}];
    

    What next?

    Well, once you have this array sorted by what you want, you have to extract their values with the map function and finnaly you will have your two arrays sorted by the same criteria:

    The code:

    // your arrays
    sortThis=[3,1,2];
    sortAccording=["With 3","With 1","With 2"];
    
    // the zip function    
    function zip(a,b) {
        return a.map(function(aa, i){ return { i: aa, j: b[i]};} )
    };               
    
    // ziping and sorting the arrays
    var zipped = zip(sortThis, sortAccording);
    zippedAndSorted = zipped.sort(function(a,b){ return a.i - b.i; });
    
    // your two sorted arrays
    sortedThis = zippedAndSorted.map(function(a){ return a.i;});
    sortedAccording = zippedAndSorted.map(function(a){ return a.j;});
    

    You also can see it working here: http://jsfiddle.net/lontivero/cfpcJ/

    Good luck!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays. The first array contains the sort order. The second array
I have two arrays, the first one contents numbers and the second one use
I have two arrays, one is very large (more than million entries) and other
I have two arrays, like this: $array1 = array( 'ADAIR', 'ADAM', 'ADAMINA', 'ADDISON', 'ADDY',
I have two arrays with the list of images name like this array 1
I have two arrays: 1. first array: array 0 => array 'id' => int
I have two arrays. The first array contains some values while the second array
I have two arrays. The first one contains of places and some information about
I have two arrays. I'd like to copy ranges of data from one of
I have two arrays and I post the arrays in one input; <input name='sistem[]'

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.