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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T07:50:06+00:00 2026-06-12T07:50:06+00:00

I have an array (let’s call it the orderArray ) with these elements: 16

  • 0

I have an array (let’s call it the orderArray) with these elements:
16 | 18 | 24 | 31 | 33

I have another array (let’s call it workingArray) with these elements:
16 | 53 | 24 | 58 | 31 | 18

The resultArray could be as follows:
[16, 53, 18, 24, 58, 31], or [16, 18, 53, 24, 58, 31] for example

resultArray should have all the elements from workingArray but with a sorting order that doesn’t conflict with orderArray.
Please note

  • orderArray and workingArray can have different elements between each other
  • elements are unique within the array

I would be really happy if there is already a function/library that does that – I have already tried _.union but that doesn’t do the job.

Short of some already-made code, what’s the easiest algorithm to achieve that?

Thanks.

EDIT: the sort order of the elements in resultArray should be changed as less as possible – changes should be strictly done not to conflict with the order in orderArray.

  • 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-12T07:50:08+00:00Added an answer on June 12, 2026 at 7:50 am

    main idea:

    orderArray.filter(inWorkingArray).concat(workingArray.filter(notInOrderArray))
    

    (copy orderArray ignoring items not in the working array, then tack on items in the workingArray that weren’t in the order array)

    you can use indexOf to test membership, or a more efficient implementation (making all the elements keys in an object, which will implicitly convert them to strings, making that optimization not work with non-primitive objects). Below is the simplest example with just indexOf, which should generalize even to non-primitive objects like sub-arrays:

    var inWorkingArray = function(x) {return workingArray.indexOf(x)!=-1};
    var notInOrderArray = function(x) {return orderArray.indexOf(x)==-1};
    

    result:

    [16, 18, 24, 31, 53, 58]
    

    That’s a good solution, but the order of elements in the result array shouldn’t be changed so much – we should try to keep the workingArray with an order as much as the original – sorry I wasn’t very clear on that. Please see my edit in the question. –OP

    The question is still not well defined, but this will sort the subset of the workingArray according to the orderArray, leaving elements not in the orderArray in place:

    var intersection = orderArray.filter(inWorkingArray);
    var c=0;
    workingArray.map(function(x){
        return notInOrderArray(x) ? x : intersection[c++];
    });
    

    result:

    [16, 53, 18, 58, 24, 31]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have an array, @theArr, which holds 1,000 or so elements such
I have an array with let's say, 500 elements. I know I can select
I have an array, let's call it $menus, that contains something like this: Array(
Let's say I have an array of elements for which a total ordering exists.
So let's say I have an array of strings in C#, I could convert
Let's say I have an array of n elements(numbers or words), I want to
Let's say we have an array of objects $objects. Let's say these objects have
Let say I have an array of 1,000,000 elements with about 90% of 0s
Let's say I have array of bytes: byte[] arr = new byte[] { 0,
Let say I have an array like: Array ( [0] => Array ( [Data]

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.