简体   繁体   中英

how to sort 2 arrays and compare values in it

I have 2 arrays i want to sort those arrays and compare it

var A = [1,5,8,0,9];
var B = [5,9,0,1,8];
for(i=0;i<A.length;i++)
{
if(A[i] == B[i]){message}else{Fail}

I want to sort those arrays and then compare the values

You can just call the sort method on an array:

 var A = [1, 5, 8, 0, 9]; var B = [5, 9, 0, 1, 8]; A.sort() B.sort() for (i = 0; i < A.length; i++) { if (A[i] == B[i]) { document.write(A[i] + ' and ' + B[i] + ' are identical' + '<br>') } else { document.write('fail') } }

You can use the sort() for this . Arary sort() I cant properly understand whats the aim behind the comparison.SO make necessary changes.

var A = [1,5,8,0,9];
A.sort();
var B = [5,9,0,1,8];
B.sort();

for(i=0;i<A.length;i++)
{
     if(A[i] == B[i])
      {
            message
      }
      else 
      {
             Fail
      }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM