I was wondering if someone could explain to me how to merge two arrays so that one is not simply appended to the other, but they need to be merged into one sorted array. I could simply append the two arrays and then sort the result, but that would be too easy. Instead I need to have my javascript look over each element of both arrays to see which number is smaller to merge into one sorted array. I need to be able to understand how and why it works if possible.

For example:

arr1 = [1,2,7,10];
arr2 = [3,4,9,19];
//needs to merge into one array appearing as [1,2,3,4,7,9,10,19];

Any quick explanation on how to do this would be greatly appreciated. Have a wonderful holiday weekend everyone and I look forward to talking with someone soon.

Use the sort() method, this will sort an Array alphabetically. If you would like to create your own sorting criteria, supply a function for the sort method to call. Sort will pass your function (a,b). If a is less than b then return -1, if a is equal to b then return 0, if a is greater than b then return 1. Sort will take it from there.

var myArray=[8,10,50,5,7,83,24,19,168];
myArray.sort();
document.writeln(myArray); // 10,168,19,24,5,50,7,8,83 (sorted alphabetically)

myArray.sort( function (a,b) { return a-b }); //Sort Numerically
document.writeln(myArray); //5,7,8,10,19,24,50,83,168

function compare(a, b) { // psudeo code. 
if (a < b) {  return -1; } 
if (a > b) {  return 1; } 
if (a == b) {  return 0; } } 
myArray.sort(compare);

Once again, thank you for the information essential. I completely understand everything you have done above. However, do you know how I can merge the two arrays together into one sorted array without combining the two arrays first and then sorting. This is where my problem is. My original plan was to simply use the concat, join, or push method to join the two arrays and then sort afterwards, however I am not supposed to do it this way. I just need a little push in the right direction on this.

there is a builtin javascript array.push method, why dont you just use it? iterate through the second array using a for loop, push all the elements to the first array, then call the sort method in the first array? isnt it clear?

there is a builtin javascript array.push method, why dont you just use it? iterate through the second array using a for loop, push all the elements to the first array, then call the sort method in the first array? isnt it clear?

Serkansendur, thank you for your response. Yes I understand that very clearly. That was my original thought on this problem. However, I am not supposed to do it that way. I need to have my javascript merge the two arrays into one sorted array without appending the two arrays and then sorting the result. This is why I am having a difficult time understanding how to do this. Any suggestions?? Thanks for your time and assistance. Have a great holiday weekend.

I was wondering if someone could explain to me how to merge two arrays so that one is not simply appended to the other, but they need to be merged into one sorted array. I could simply append the two arrays and then sort the result, but that would be too easy. Instead I need to have my javascript look over each element of both arrays to see which number is smaller to merge into one sorted array. I need to be able to understand how and why it works if possible.

For example:

arr1 = [1,2,7,10];
arr2 = [3,4,9,19];
//needs to merge into one array appearing as [1,2,3,4,7,9,10,19];

Any quick explanation on how to do this would be greatly appreciated. Have a wonderful holiday weekend everyone and I look forward to talking with someone soon.

There are better ways to understand sorting than doing things the hard way. Anyways, what you can do is:

- Sort both the arrays
- Inspect the first element of both the arrays
- The array which has the smallest element as its first element is the one into which the other array will be merged. Let A1 be the array into which array A2 will be merged.
- Run a loop over the array A2 and for each element; let that element be E.
- Run a loop over A1 and find an appropriate place in A1 to insert E
- Rinse and repeat

Of course the above would require a lot of array shifting to accommodate the newly inserted elements. For all practical purposes, merging the two arrays and then sorting them would be far less messy and far more elegant solution.

Another thing you can do is to consider the separate arrays as one consecutive one and then use your favorite sorting algorithm to move around elements assuming they belong to the same array. But then again, this method also suffers the same problem of being messy and much less elegant.

SOS, this indeed gave me a good start to solving this problem. The toughest part was getting my algorithm to work properly with me being new to javascript. I do understand that this is not the practical way to complete this task, but this was the way in which I did need to do it. This problem has actually taught me quite a bit about arrays, sorting, etc... I ended up creating a new array and merging both of these arrays into the new empty array in the proper order once the first two arrays were sorted. Anyhow, thanks to everyone who contributed towards helping me with this problem. Have a wonderful holiday season and take care everyone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.