-2

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

user3724559
  • 219
  • 1
  • 6
  • 20
  • Thing you are doing is far away from sort – Shailendra Sharma Nov 06 '15 at 09:19
  • So, which part do you have trouble with? Sorting or [comparing](http://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript)? – DarkDust Nov 06 '15 at 09:20
  • You can calll the `sort` method on an array: `A.sort()`. See http://www.w3schools.com/jsref/jsref_sort.asp – Alex Nov 06 '15 at 09:21
  • i tried .sort , it dint work – user3724559 Nov 06 '15 at 09:21
  • I want to sort both arrays – user3724559 Nov 06 '15 at 09:21
  • BTW people, the question is bad, but it's not "unclear what you're asking". The question is straight-forward but lacks effort. – DarkDust Nov 06 '15 at 09:22
  • So? Why don't you sort them then? Lack of documentation/examples out there? – DarkDust Nov 06 '15 at 09:23
  • I wrote some code , its not the actual code – user3724559 Nov 06 '15 at 09:23
  • Come one, you've asked over 20 questions here already, you should know the deal by now: *provide more info!* What did you try _exactly_? What do you expect to happen? What is happening? Remember: the quality of answers is almost directly proportional to the quality of the question: if you don't put any effort in your question, why should we put effort into answering it? – DarkDust Nov 06 '15 at 09:25
  • Possible duplicate of [Java Array Sort descending?](http://stackoverflow.com/questions/1694751/java-array-sort-descending) – Aurasphere Nov 06 '15 at 09:45

2 Answers2

0

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')
  }
}
Alex
  • 21,273
  • 10
  • 61
  • 73
0

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
      }
}
Krishna Chandran
  • 389
  • 3
  • 18