Check if Javascript array values ​​are in ascending order

Let's say I have an array of integers in Javascript, and I would like to check if all its values ​​are in ascending order. I want to save the array key in another array if the algorithm finds a value that is lower (or equal), not only comparing the nearest previous one, but also comparing any value that is before it. I have done this:

arr = [], nonvalid = []; for (var j = 1; j < arr.length; j++){ if ( arr[j+1] <= arr[j] ){ nonvalid.push(j); } } 

Obviously, the above algorithm only checks for values ​​that are lower than the previous ones.

An array may contain the following values:

arr = 1, 2, 3, 10, 5 , 11, 12, 2 , 4 , 25

Invalid values ​​are bold. If I run the above cycle, he will not “catch” the second last ( 4 ), because he will be larger than his closest left brother, but not as high as all his left brothers.

EDIT:

Tried the following solutions, and none of them returned all invalid values ​​for this array, except mine .:(

They returned the last two values ​​correctly, but not the second. However, I do not understand why.

[24398, 24397 , 25004, 25177, 26302, 28036, 29312, 29635, 29829, 30476, 32595, 33732, 34995, 36047, 36363, 37310, 38022, 38882, 40746, 41212, 42846, 43588, 44029, 44595, 44846 , 45727, 46041, 47293, 48002, 48930, 49858, 51184, 51560, 53895, 54247, 54614, 55713, 56813, 57282, 57480, 57875, 58073, 58403, 60321, 61469, 62051, 62310, 62634, 63217, 643217, 643217. , 65413, 65677, 65940, 66203, 66572, 67957, 68796, 68964, 69098, 69233, 69435, 69759, 71496, 72577, 72823, 73007, 73252, 73743, 73866, 76405, 77037, 77416, 77669, 79691, 8088. , 81339, 81794, 82067, 82431, 83244, 84861, 86836, 88632, 89877, 90296, 91049, 91885, 92351, 92614, 93141, 93733, 93930, 94531, 95206, 95882, 96895, 97732, 97973, 99261, 99261, 99261, 99261, 99261. , 99583, 100332, 100599, 101666, 102066, 102600, 103504, 104432, 105174, 107216, 109085, 110181, 110679, 111177, 111988, 112553, 113005, 113457, 600 , 600 ]

+8
javascript jquery arrays
source share
8 answers

When you find the item is out of order, look at the following items until they fail with respect to the item before non-standard.

Add outer order elements to the second array and continue the new element in order.

 var outs= [], L= A.length, i= 0, prev; while(i<L){ prev= A[i]; while(A[++i]<prev) outs.push(i); } alert(outs) 
+2
source share

Keep track of the highest value you saw ( see fiddle ):

 function find_invalid_numbers(arr) { var nonvalid, i, max; nonvalid = []; if (arr.length !== 0) { max = arr[0]; for (i = 1; i < arr.length; ++i) { if (arr[i] < max) { nonvalid.push(arr[i]); } else { max = arr[i]; } } } return nonvalid; } 
+9
source share

Why not compare with the last known good number ?

 var arr = [1, 2, 3, 10, 5, 11, 12, 2, 4, 25], nonvalid = [], lastGoodValue = 0; for (var j = 1; j < arr.length; j++) { if (j && arr[j] <= lastGoodValue) { //if not the first number and is less than the last good value nonvalid.push(arr[j]); } else { //if first number or a good value lastGoodValue = arr[j]; } } console.log(arr, nonvalid) 
+3
source share

Demo

 var arr = [24398, 24397, 25004, 25177, 26302, 28036, 29312, 29635, 29829, 30476, 32595, 33732, 34995, 36047, 36363, 37310, 38022, 38882, 40746, 41212, 42846, 43588, 44029, 44595, 44846, 45727, 46041, 47293, 48002, 48930, 49858, 51184, 51560, 53895, 54247, 54614, 55713, 56813, 57282, 57480, 57875, 58073, 58403, 60321, 61469, 62051, 62310, 62634, 63217, 64505, 65413, 65677, 65940, 66203, 66572, 67957, 68796, 68964, 69098, 69233, 69435, 69759, 71496, 72577, 72823, 73007, 73252, 73743, 73866, 76405, 77037, 77416, 77669, 79691, 80885, 81339, 81794, 82067, 82431, 83244, 84861, 86836, 88632, 89877, 90296, 91049, 91885, 92351, 92614, 93141, 93733, 93930, 94531, 95206, 95882, 96895, 97732, 97973, 99261, 99422, 99583, 100332, 100599, 101666, 102066, 102600, 103504, 104432, 105174, 107216, 109085, 110181, 110679, 111177, 111988, 112553, 113005, 113457, 600, 600], nonvalid = [], max = arr[0]; for(var j=0; j<arr.length; j++){ var test= arr[j+1]<=max ? nonvalid.push(arr[j+1]) : max=arr[j]; } alert(nonvalid); // 24397, 600, 600 
+1
source share

A simple functional way to do this inline without loops or variables:

 arr.filter(function(a,b,c){ return Math.max.apply(Math, c.slice(0,b)) > a ; }); 
+1
source share

Another very good functional way to do this:

 var isAscending = a => a.slice(1) .map((e,i) => e > a[i]) .every(x => x); console.log(isAscending([1,2,3,4])); console.log(isAscending([1,2,5,4])); 
+1
source share

Copy the array first, delete any element that does not match the order of array.splice(index, 1) , and continue. Thus, any element must be larger than the one in front of it, but the one in front of it will always be maximum.

0
source share

Answering my question, answering your questions, I tried the following survey. He seems to be doing his job, but she goes a little too far.

for (var i = 0; i <arr.length; i ++) {

 for (var j = 1; j < arr.length; j++){ if ( arr[j] > 0 && arr[i] > 0 && j != i ){ if ( arr[j] <= arr[i] && j > i ){ if ( jQuery.inArray(j, nonvalid) == - 1) nonvalid.push(j); } } } } 
0
source share

All Articles