Pretty easy 3 stages. Combine the two arrays, remove duplicates (except for 1 copy), then sort.
PHP Code:
var list1 = "a,b,c,d,e,f";
var list2 = "d,f,w,x,y";
var list1split = list1.split(',');
var list2split = list2.split(',');
var combined_raw = new Array();
var combined_unique = new Array();
var combined_final = new Array();
var i;
var x;
//append list1split and list2split into combined_raw
for (i = 0; i < list1split.length; i++) { combined_raw[combined_raw.length] = list1split[i]; }
for (i = 0; i < list2split.length; i++) { combined_raw[combined_raw.length] = list2split[i]; }
//turn combined_raw into combined_unique, omitting duplicates
for (i = 0; i < combined_raw.length; i++) {
var found = false;
for (x = 0; x < combined_unique.length; x++) {
if (combined_unique[x] == combined_raw[i]) { found = true; break; }
}
if (!found) {
combined_unique[combined_unique.length] = combined_raw[i];
}
}
//sort combined_unique into combined_final
combined_final = combined_unique.sort();
Thanks for the response. I like the solution, 2 questions if I may ask, how could I make it work for IE? it works for firefox ok but not for my IE browser. How could I use it if the two list values come from input textbox. Appreciate your help.
Thanks for the response. I like the solution, 2 questions if I may ask, how could I make it work for IE? it works for firefox ok but not for my IE browser. How could I use it if the two list values come from input textbox. Appreciate your help.
Originally Posted by jt107
Thanks for your help. This one works perfectly. Appreciate your help!
Just saw you questions...
then assuming you are referring to post #3 ...
AFAIK, the solution does work in MSIE. Might be your set-up, but I assume you have solved this portion because of the next post.
Conversion of a string to an array is relatively simple:
Code:
// one possibility
var list = 'a b c d e f'.split (' ');
// or even
var list = 'abcde'.split('');
You should be able to acquire the text from the <input> element using .value syntax.
Again, I assume you have solved this as well because of the last post.
Therefore, you're most welcome, I'm sure, from both of use regardless of which solution you use.
Happy to help.
Good Luck!
BTW: You should mark thread as resolved if you are satisfied with the solutions presented.
Could you show me how could I make it work with space demiliter list?
list1 = "a b c d e f";
list2 ="d f w x y";
list=(list1+" "+list2).split(/ /g).sort().join(' ').replace(/(s[^s]+)\1/g,'$1');
alert(list);
Bookmarks