Solved

Comparing two array and sorting the array through javascript

Posted on 2004-10-11
2
604 Views
Last Modified: 2012-08-14
Hi,
   I have 2 array ex: arr1,arr2,
   in arr1 it has 4 values ex : 1,5,9,10
   in arr2 it has 12 values ex : 1,2,3,4,5,6,7,8,9,10,11,12

how to compare these 2 array and filter the arr1 from arr2 ie i want the values that are not in first array but found in 2nd array.
ie i want the values : 2,3,4,6,7,8,11,12

Thanx




0
Comment
Question by:mohan21_kumar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 11

Accepted Solution

by:
pratap_r earned 20 total points
ID: 12277273
hope this will suffice...

a=new Array(5,1,9,10);
b=new Array(1,2,3,4,5,6,7,8,9,10,11,12);

a.sort(sortfunc); //sort using a custom callback
b.sort(sortfunc);

c=new Array();
for(i=0; i < b.length;i++)c[i]=b[i];//make a copy of the array

for(i=0; i < a.length;i++)
{
      for(j=0; j < c.length;j++)
      {
            if(c[j]==a[i])c.splice(j,1);//there is a match, remove the item from the list
      }
}
alert(c); //shows 2,3,4,6,7,8,11,12

//our custom callback for sorting
function sortfunc(item1,item2)
{
    if (parseInt(item1) < parseInt(item2))return -1;
    if (parseInt(item1) > parseInt(item2))return +1;
      return 0;
}


Enjoy!
Pratap
0
 

Expert Comment

by:MexicanHeat
ID: 12580622
Pratap r's answer should work, but if youre looking for a better understanding of the sort itself try this link:

http://cplusplus.com/ref/cstdlib/qsort.html

http://www.pscode.com/vb/scripts/ShowCode.asp?txtCodeId=6646&lngWId=3
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question