943,513 Members | Top Members by Rank

Ad:
Nov 26th, 2008
0

Merging 2 Arrays

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
jobojo is offline Offline
28 posts
since Oct 2008
Nov 26th, 2008
0

Re: Merging 2 Arrays

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.

javascript Syntax (Toggle Plain Text)
  1. var myArray=[8,10,50,5,7,83,24,19,168];
  2. myArray.sort();
  3. document.writeln(myArray); // 10,168,19,24,5,50,7,8,83 (sorted alphabetically)
  4.  
  5. myArray.sort( function (a,b) { return a-b }); //Sort Numerically
  6. document.writeln(myArray); //5,7,8,10,19,24,50,83,168
  7.  
  8. function compare(a, b) { // psudeo code.
  9. if (a < b) { return -1; }
  10. if (a > b) { return 1; }
  11. if (a == b) { return 0; } }
  12. myArray.sort(compare);
Featured Poster
Reputation Points: 114
Solved Threads: 138
Posting Shark
essential is offline Offline
973 posts
since Aug 2008
Nov 26th, 2008
0

Re: Merging 2 Arrays

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
jobojo is offline Offline
28 posts
since Oct 2008
Nov 26th, 2008
0

Re: Merging 2 Arrays

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?
Featured Poster
Reputation Points: 854
Solved Threads: 127
Banned
serkan sendur is offline Offline
2,057 posts
since Jan 2008
Nov 26th, 2008
0

Re: Merging 2 Arrays

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
jobojo is offline Offline
28 posts
since Oct 2008
Nov 27th, 2008
0

Re: Merging 2 Arrays

Click to Expand / Collapse  Quote originally posted by jobojo ...
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:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. - Sort both the arrays
  2. - Inspect the first element of both the arrays
  3. - 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.
  4. - Run a loop over the array A2 and for each element; let that element be E.
  5. - Run a loop over A1 and find an appropriate place in A1 to insert E
  6. - 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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Dec 1st, 2008
0

Re: Merging 2 Arrays

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
jobojo is offline Offline
28 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: JavaScript Newbie - problem with style="display:" toggle
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: validation





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC