Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Oct 2008
Posts: 19
Reputation: jobojo is an unknown quantity at this point 
Solved Threads: 0
jobojo jobojo is offline Offline
Newbie Poster

Merging 2 Arrays

 
0
  #1
Nov 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 954
Reputation: essential will become famous soon enough essential will become famous soon enough 
Solved Threads: 131
Featured Poster
essential's Avatar
essential essential is offline Offline
Posting Shark

Re: Merging 2 Arrays

 
0
  #2
Nov 26th, 2008
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.

  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);
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 19
Reputation: jobojo is an unknown quantity at this point 
Solved Threads: 0
jobojo jobojo is offline Offline
Newbie Poster

Re: Merging 2 Arrays

 
0
  #3
Nov 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Merging 2 Arrays

 
0
  #4
Nov 26th, 2008
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?
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 19
Reputation: jobojo is an unknown quantity at this point 
Solved Threads: 0
jobojo jobojo is offline Offline
Newbie Poster

Re: Merging 2 Arrays

 
0
  #5
Nov 26th, 2008
Originally Posted by serkansendur View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,600
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 462
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Merging 2 Arrays

 
0
  #6
Nov 27th, 2008
Originally Posted by jobojo View 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.
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 19
Reputation: jobojo is an unknown quantity at this point 
Solved Threads: 0
jobojo jobojo is offline Offline
Newbie Poster

Re: Merging 2 Arrays

 
0
  #7
Dec 1st, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC