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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:49:10+00:00 2026-06-15T11:49:10+00:00

I have two Javascript array objects, where first one is sorted in the order

  • 0

I have two Javascript array objects, where first one is sorted in the order I want it to be sorted in and second array is sorted in the wrong order, for example:

1) SortedArray (0 - 10 items, 11 - 20 items, 21 - 30 items, 10000 - 20000 items)

2) Alphabetically sorted Array (0 - 10 items, 10000 - 20000 items, 11 - 20 items, 21 - 30 items)

I would like to sort the second array in the same order as the first one, is there an easy way to solve this issue?

Any help would be appreciated. Thank you.

Conditions:

1) The second array can be different in size. So it could have less values than the first array.

2) If the data is corrupt, the second array can have values that do not exist in the first array.

Update

I have built an sample here based on Bergi’s response: jsfiddle.net/EPwS6 – but now I need to figure out how I can preserve values in arr2 which do not exist in arr1. Any ideas or tips?

  • 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-15T11:49:12+00:00Added an answer on June 15, 2026 at 11:49 am

    If both arrays contain the same items, but in different order, it’s easy: Just copy the first array to the second. Either use a direct reference, or slice to create a new array.

    If the two arrays contain different objects with similar keys, it gets more difficult. Yet you can easily manage that by creating a lookup table:

    var table = {};
    for (var i=0; i<arr2.length; i++)
        table[ arr2[i].getKey() ] = arr2[i];
    for (var i=0; i<arr1.length; i++)
        arr2[i] = table[ arr1[i].getKey() ];
    table = null;
    // arr2 now ordered by the same keys as arr1, and its length is set to arr1.length
    

    If the key sets are not the same, you might go with something like this:

    var table = {};
    for (var i=0; i<arr2.length; i++)
        table[ arr2[i].getKey() ] = arr2[i];
    for (var i=0; i<arr1.length; i++) {
        var key = arr1[i].getKey();
        if (key in table) {
            arr2.push(table[key]); // add to array
            delete table[key]; // and prevent readding
        }
    }
    for (var key in table)
        arr2.push(table[key]); // add all leftover objects
    table = null;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two dates in javascript: var first = '2012-11-21'; var second = '2012-11-03';
I have two or more javascript objects. I want to merge them adding values
I have two javascript objects: var a = { x: 1, y: { faz:
I have created two JavaScript files. One file is "validators.js" and the other is
I'm studying javascript for the first time and I've some doubt: I have two
I have a complicated example of converting a Javascript Array to a JSON String.
I have to send a two-dimensional JavaScript Array to a PHP page. Indeed, I'm
I have an array of Objects known as players, which is sorted often (so
I want to break down a JSON string into smaller objects. I have two
I have two arrays of JavaScript Objects that I'd like to compare to see

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.