Solved

Comparing two array and sorting the array through javascript

Posted on 2004-10-11
2
594 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
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now