Documentation

issorted

Determine whether set elements are in sorted order

Syntax

TF = issorted(A)
TF = issorted(A, 'rows')

Description

TF = issorted(A) returns logical 1 (true) if the elements of A are in sorted order, and logical 0 (false) otherwise. Input A can be a vector or an N-by-1 or 1-by-N cell array of strings. A is considered to be sorted if A and the output of sort(A) are equal.

TF = issorted(A, 'rows') returns logical 1 (true) if the rows of two-dimensional matrix A are in sorted order, and logical 0 (false) otherwise. Matrix A is considered to be sorted if A and the output of sortrows(A) are equal.

    Note   Only the issorted(A) syntax supports A as a cell array of strings.

    The issorted(A,'rows') syntax supports A as a categorical array, but not as a cell array of strings.

Examples

Example 1 — Using issorted on a vector

A = [5 12 33 39 78 90 95 107 128 131];

issorted(A)
ans = 
     1

Example 2 — Using issorted on a matrix

A = magic(5)
A =
    17    24     1     8    15
    23     5     7    14    16
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9

issorted(A, 'rows')
ans = 
     0

B = sortrows(A)
B =
     4     6    13    20    22
    10    12    19    21     3
    11    18    25     2     9
    17    24     1     8    15
    23     5     7    14    16

issorted(B)
ans = 
     1

Example 3 — Using issorted on a cell array

x = {'one'; 'two'; 'three'; 'four'; 'five'};
issorted(x)
ans =
     0

y = sort(x)
y = 
    'five'
    'four'
    'one'
    'three'
    'two'

issorted(y)

More About

collapse all

Tips

For character arrays, issorted uses ASCII, rather than alphabetical, order.

You cannot use issorted on arrays of greater than two dimensions.

Introduced before R2006a

Was this topic helpful?