| | |
Merging 2 Arrays
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:
Solved Threads: 0
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.
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.
javascript Syntax (Toggle Plain Text)
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);
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
•
•
•
•
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?
•
•
•
•
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.
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
- 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
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.
•
•
Join Date: Oct 2008
Posts: 19
Reputation:
Solved Threads: 0
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.
![]() |
Similar Threads
- Need refactoring opinions (Java)
- Merging two files? (C)
- just a little help needed (C++)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: JavaScript Newbie - problem with style="display:" toggle
- Next Thread: validation
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxexample ajaxhelp ajaxjspservlets animate automatically beta box browser bug captchaformproblem checkbox close codes createrange() css cursor debugger decimal dependent disablefirebug dom download dropdown editor element engine enter error events explorer ext file firefox form forms frameworks getselection google gwt gxt hiddenvalue highlightedword hint html htmlform ie8 iframe index internet java javascript javascripthelp2020 jawascriptruntimeerror jquery jsf jsfile jsp jump listbox maps masterpage math media menu microsoft mp4 object onmouseoutdivproblem onreadystatechange paypal pdf php player programming progressbar prototype redirect regex runtime safari scale scriptlets search security select size software sql text textarea unicode w3c window windowofwords windowsxp wysiwyg \n






