Documentation

This is machine translation

Translated by Microsoft
Mouse over text to see original. Click the button below to return to the English verison of the page.

issorted

Determine whether array is sorted

Syntax

Description

example

TF = issorted(A) returns a logical scalar 1 (true) when the elements of a vector are listed in ascending order and 0 (false) otherwise.

example

TF = issorted(A,'rows') returns 1 when the elements of the first column of a matrix are sorted and 0 otherwise. If the first column contains repeated elements, then issorted looks at the ordering of the second column to determine TF. In general, issorted looks to the column immediately to the right to determine TF when the current and previous columns have repeated elements.

For more information on sorted character and string arrays, see Sort Order for Character and String Arrays.

This syntax is not supported for a matrix of character vectors.

example

TF = issorted(str) returns 1 when the elements of a string array are listed in ascending order and 0 otherwise. A 2-D array of strings is sorted when each of the columns is sorted. A multidimensional array of strings is sorted when the first array dimension whose size does not equal 1 is sorted.

example

TF = issorted(str,dim) returns 1 when str is sorted along the dimension dim. For example, if str is a matrix, then issorted(str,2) operates across the columns of str. If the elements in each row are sorted from left to right, then issorted returns 1.

example

TF = issorted(___,direction) returns 1 when str is sorted according to direction. The operating direction can be 'ascend' (default) for ascending order or 'descend' for descending order.

Examples

collapse all

Create a vector and check if it is sorted in ascending order.

A = [5 12 33 39 78 90 95 107];
issorted(A)
ans =

  logical

   1

Create a 5-by-5 matrix and check if the rows are sorted in ascending order according to the first column's elements.

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 =

  logical

   0

Sort the rows of A using the sortrows function and check that the rows are sorted.

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,'rows')
ans =

  logical

   1

Create a 2-D array of strings and determine if each column is sorted.

str = string({'Horse','Chicken';'cow','Goat'})
str = 

  2×2 string array

    "Horse"    "Chicken"
    "cow"      "Goat"   

issorted(str)
ans =

  logical

   1

Determine if the rows are sorted from left to right.

issorted(str,2)
ans =

  logical

   0

Determine if each row is sorted in descending order from left to right.

issorted(str,2,'descend')
ans =

  logical

   1

Input Arguments

collapse all

Input array, specified as a vector, matrix, cell array of character vectors, or timetable.

  • If A is a timetable, then issorted returns 1 if the associated time vector is sorted.

  • If A contains NaN, NaT, or undefined categorical values, then issorted treats them like large numbers.

  • If A is complex, then TF is based on the magnitude of the elements. If more than one element has equal magnitude, then TF is based on phase angle in the interval [-π, π].

  • If A is a cell array of character vectors, then TF is based on the code order for the UTF-16 character encoding scheme. The sort is case-sensitive.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64 | logical | char | cell | categorical | datetime | duration | timetable

Complex Number Support: Yes

Input string array, specified as a vector, matrix, or multidimensional array. TF is based on the code order for the UTF-16 character encoding scheme. The sort is case-sensitive.

If str contains missing string values, then issorted treats them like large numbers.

Data Types: string

Dimension to operate along, specified as a positive integer scalar. If not value is specified, the default is the first array dimension whose size does not equal 1.

Data Types: single | double | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64

Sorting direction, specified as 'ascend' for ascending order or 'descend' for descending order.

Data Types: char | string

More About

collapse all

Sort Order for Character and String Arrays

MATLAB® stores characters as Unicode® using the UTF-16 character encoding scheme. Character and string arrays are sorted according to the UTF-16 code point order. For the characters that are also the ASCII characters, this order means that uppercase letters come before lowercase letters. Digits and some punctuation also come before letters.

See Also

|

Introduced before R2006a

Was this topic helpful?