Skip to content

Instantly share code, notes, and snippets.

@yorkie
Last active May 18, 2022 06:07
Show Gist options
  • Save yorkie/7959685 to your computer and use it in GitHub Desktop.
Save yorkie/7959685 to your computer and use it in GitHub Desktop.
Check if an array is sorted in Javascript...
/*
* check the array is sorted
* return: if positive -> 1
* if negative -> -1
* not sorted -> 0
*/
Array.prototype.isSorted = function() {
return (function(direction) {
return this.reduce(function(prev, next, i, arr) {
if (direction === undefined)
return (direction = prev <= next ? 1 : -1) || true;
else
return (direction + 1 ?
(arr[i-1] <= next) :
(arr[i-1] > next));
}) ? Number(direction) : false;
}).call(this);
}
var arr = [3,2,1,0];
arr.isSorted();
@abgd23
Copy link

abgd23 commented Mar 24, 2015

great, exactly what im looking for.. thank you!

Copy link

ghost commented Apr 7, 2015

This looks promising. But for an array with 100 integers in it, e.g. [99, 99, 98, ... , 94, 94, 93, 97] it keeps returning 1, when it should actually return 0. For a smaller sized array it works fine.

@razchiriac
Copy link

[107, 956, -135, 598] returns 1

@theQuazz
Copy link

> !![3,2,3,4,5,6,7,8].reduce((memo, item) => memo && item >= memo && item)
false
> !![1,2,3,4,5,6,7,8].reduce((memo, item) => memo && item >= memo && item)
true
> !![2,2,3,4,5,6,7,8].reduce((memo, item) => memo && item >= memo && item)
true

@maxint137
Copy link

@theQuazz - awesome!

@hieutdo
Copy link

hieutdo commented Sep 15, 2017

/**
  * Returns true of false, indicating whether the given array of numbers is sorted
  *  isSorted([])                        // true
  *  isSorted([-Infinity, -5, 0, 3, 9])  // true
  *  isSorted([3, 9, -3, 10])            // false
  *
  * @param {number[]} arr 
  * @return {boolean}
  */
function isSorted(arr) {
  const limit = arr.length - 1;
  return arr.every((_, i) => (i < limit ? arr[i] <= arr[i + 1] : true));
}

@mattdiamond
Copy link

mattdiamond commented Jan 2, 2018

@theQuazz

> !![0, 2, 4, 6].reduce((memo, item) => memo && item >= memo && item)
false

:(

@hieudt

Good solution! You can also avoid the limit calculation and write:

myArray.every((val, i, arr) => !i || (val >= arr[i - 1]));

@fuchao2012
Copy link

thx @mattdiamond

/**
 * @name 是否是有序数组
 * @param array
 * @extends Array.every
 * @example 
 * isSorted([0, 1, 2, 2]); // 1
 * isSorted([4, 3, 2]); // -1
 * isSorted([4, 3, 5]); // 0
 */
export const IsSorted = (array: any[]): number => {
    return array.every((value, index) => !index || (value >= array[index - 1]))
        ? 1 : array.every((value, index) => !index || (value <= array[index - 1]))
            ? -1 : 0;
};

@roman-lezhnin
Copy link

function isSorted(array) {
  const limit = array.length - 1;
  for (let i = 0; i < limit; i++) {
    const current = array[i], next = array[i + 1];
    if (current > next) { return false; }
  }
  return true;
}

@qialex
Copy link

qialex commented May 1, 2019

Inspired by @theQuazz

!![-Infinity,-1,0,2,3,Infinity].reduce((memo, item) => 'boolean' !== typeof memo && item >= memo && item) // true

!![-Infinity,-1,0,2,3,Infinity,2].reduce((memo, item) => 'boolean' !== typeof memo && item >= memo && item) // false

!![0,2,3,4].reduce((memo, item) => 'boolean' !== typeof memo && item >= memo && item) // true

!![0,2,3,4,2].reduce((memo, item) => 'boolean' !== typeof memo && item >= memo && item) // false

ES6

let arr = [-Infinity,-1,0,2,3,Infinity]

arr.slice(1).every((item, i) => arr[i] <= item) // true


arr = [-Infinity,-1,0,2,3,Infinity,2]

arr.slice(1).every((item, i) => arr[i] <= item) // false

@abbasi-naim
Copy link

abbasi-naim commented Nov 29, 2021

function isSorted(boxNumbers) {
  for (let i = 1; i < boxNumbers.length; i++) {
    if (boxNumbers[i - 1] > boxNumbers[i]) {
      return false;
    }
  }
  return true;
}

(formatted by @yorkie)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment