Documentation

sort

Sort array elements

Syntax

Description

example

B = sort(A) sorts the elements of A in ascending order along the first array dimension whose size does not equal 1.

  • If A is a vector, then sort(A) sorts the vector elements.

  • If A is a matrix, then sort(A) treats the columns of A as vectors and sorts each column.

  • If A is a multidimensional array, then sort(A) operates along the first array dimension whose size does not equal 1, treating the elements as vectors.

example

B = sort(A,dim) returns the sorted elements of A along dimension dim. For example, if A is a matrix, then sort(A,2) sorts the elements in each row.

example

B = sort(___,mode) returns sorted elements of A in the order specified by mode using any of the previous syntaxes. The single string, 'ascend', indicates ascending order (default) and 'descend' indicates descending order.

example

[B,I] = sort(___) additionally returns a collection of index vectors for any of the previous syntaxes. I is the same size as A and describes the arrangement of the elements of A into B along the sorted dimension. For example, if A is a numeric vector, B = A(I).

Examples

collapse all

Sort Vector in Ascending Order

Create a row vector and sort its elements in ascending order.

A = [9 0 -7 5 3 8 -10 4 2];
B = sort(A)
B =

   -10    -7     0     2     3     4     5     8     9

Sort Matrix Rows in Ascending Order

Create a matrix and sort each of its rows in ascending order.

A = [3 6 5; 7 -2 4; 1 0 -9]
A =

     3     6     5
     7    -2     4
     1     0    -9

B = sort(A,2)
B =

     3     5     6
    -2     4     7
    -9     0     1

Sort Matrix Columns in Descending Order

Create a matrix and sort its columns in descending order.

A = [10 -12 4 8; 6 -9 8 0; 2 3 11 -2; 1 1 9 3]
A =

    10   -12     4     8
     6    -9     8     0
     2     3    11    -2
     1     1     9     3

B = sort(A,'descend')
B =

    10     3    11     8
     6     1     9     3
     2    -9     8     0
     1   -12     4    -2

Sort and Index datetime Array

Create an array of datetime values and sort them in ascending order, that is, from the earliest to the latest calendar date.

ds = {'2012-12-22';'2063-04-05';'1992-01-12'};
A = datetime(ds,'Format','yyyy-MM-dd')
A = 

   2012-12-22
   2063-04-05
   1992-01-12

[B,I] = sort(A)
B = 

   1992-01-12
   2012-12-22
   2063-04-05


I =

     3
     1
     2

B lists the sorted dates and I contains the corresponding indices of A.

Access the sorted elements from the original array directly by using the index array I.

A(I)
ans = 

   1992-01-12
   2012-12-22
   2063-04-05

Sort 3-D Array

Create a 2-by-2-by-2 array and sort its elements in ascending order along the third dimension.

A(:,:,1) = [2 3; 1 6];
A(:,:,2) = [-1 9; 0 12];
A
A(:,:,1) =

     2     3
     1     6


A(:,:,2) =

    -1     9
     0    12

B = sort(A,3)
B(:,:,1) =

    -1     3
     0     6


B(:,:,2) =

     2     9
     1    12

Use A(:), the column representation of A, to sort all of the elements of A.

B = sort(A(:))
B =

    -1
     0
     1
     2
     3
     6
     9
    12

Input Arguments

collapse all

A — Input arrayvector | matrix | multidimensional array

Input array, specified as a vector, matrix, or multidimensional array.

  • If A is a scalar, then sort(A) returns A.

  • If A is an empty 0-by-0 matrix, then sort(A) returns an empty 0-by-0 matrix.

  • If A contains NaN values or undefined categorical or datetime elements, then sort(A) places them on the high end of the sort (as if they were large numbers).

  • If A is complex, then sort(A) sorts the elements by magnitude. If more than one element has equal magnitude, the elements are sorted by phase angle on the interval [−π, π].

  • If A is a string, then sort(A) sorts according to the ASCII dictionary order. The sort is case sensitive with uppercase letters appearing in the output before the lowercase letters.

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

dim — Dimension to operate alongpositive integer scalar

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

Consider a two-dimensional input array, A:

  • sort(A,1) sorts the elements in the columns of A.

  • sort(A,2) sorts the elements in the rows of A.

sort returns A if dim is greater than ndims(A). If A is a cell array, then dim is not supported.

mode — Sorting mode'ascend' (default) | 'descend'

Sorting mode, specified as 'ascend' or 'descend'. If A is a cell array, then mode is not supported.

Example: sort(A,2,'descend') sorts the rows of A in descending order.

Data Types: char

Output Arguments

collapse all

B — Sorted arrayvector | matrix | multidimensional array

Sorted array, returned as a vector, matrix, or multidimensional array the same size and type as A. The ordering of the elements in B preserves the order of any equal elements in A.

I — Sort indexvector | matrix | multidimensional array

Sort index, returned as a vector, matrix, or multidimensional array the same size as A. The index vectors are oriented along the same dimension that sort operates on. For example, if A is a 2-by-3 matrix, then [B,I] = sort(A,2) sorts the elements in each row of A. I is a collection of 1-by-3 row index vectors describing the rearrangement. If you do not specify dim, then [B,I] = sort(A) sorts the columns of A, and I is a collection of 2-by-1 column index vectors.

More About

collapse all

Tips

  • The sortrows function provides additional flexibility for subsorting over multiple columns of nonvector input arrays.

  • The sort function and the relational operators use different orderings for complex numbers. For more information, see Relational Operations.

See Also

| | | | | |

Was this topic helpful?