Documentation

This is machine translation

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

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

sort

Sort array elements

Syntax

B = sort(A)
B = sort(A,dim)
B = sort(___,direction)
B = sort(___,Name,Value)
[B,I] = sort(___)

Description

example

B = sort(A) sorts the elements of A in ascending order.

  • 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 of each row.

example

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

example

B = sort(___,Name,Value) specifies additional parameters for sorting. For example, sort(A,'ComparisonMethod','abs') sorts the elements of A by magnitude.

example

[B,I] = sort(___) also 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 vector, then B = A(I).

Examples

collapse all

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

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

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

Starting in R2016b, you can create string arrays using the string function, and sort them using the sort function. Sort strings in each column of a string array according to Unicode® dictionary order.

A = string({'Smith','Burns';...
    'Jones','Matthews';...
    'Peterson','Adams'});
B = sort(A)
B = 3x2 string array
    "Jones"       "Adams"   
    "Peterson"    "Burns"   
    "Smith"       "Matthews"

Sort the strings in each row.

B = sort(A,2)
B = 3x2 string array
    "Burns"    "Smith"   
    "Jones"    "Matthews"
    "Adams"    "Peterson"

Text processing functions (such as strfind and regexp) accept string arrays as inputs, but other functions (for example, addpath) do not.

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 = 3x1 datetime array
   2012-12-22
   2063-04-05
   1992-01-12

[B,I] = sort(A)
B = 3x1 datetime array
   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 = 3x1 datetime array
   1992-01-12
   2012-12-22
   2063-04-05

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 = 
A(:,:,1) =

     2     3
     1     6


A(:,:,2) =

    -1     9
     0    12

B = sort(A,3)
B = 
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

Sort the elements of a complex vector by their real parts. For elements with equal real parts, sort breaks the tie based on their imaginary parts.

A = [1+2i 3+i i 0 -i];
B = sort(A,'ComparisonMethod','real')
B = 
  Columns 1 through 4

   0.0000 - 1.0000i   0.0000 + 0.0000i   0.0000 + 1.0000i   1.0000 + 2.0000i

  Column 5

   3.0000 + 1.0000i

Input Arguments

collapse all

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

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

  • If A contains missing values, such as NaN, NaT, <undefined>, and missing, then sort places them at the end.

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

  • If A is a cell array of character vectors or a string array, then sort(A) sorts the elements according to the code order for the UTF-16 character encoding scheme. The sort is case-sensitive. For more information on sorting character and string arrays, see Sort Order for Character and String Arrays.

  • If A is a string array, then sort reorders the elements of the array, but does not reorder characters within the strings.

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

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

  • Consider a matrix 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). dim is not supported when A is a cell array, that is, sort only operates along the first array dimension whose size does not equal 1.

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

Sorting direction, specified as 'ascend' or 'descend'. direction is not supported when A is a cell array, that is, sort only sorts in ascending order.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: sort(A,'MissingPlacement','last')

collapse all

Placement of missing values (NaN, NaT, <undefined>, and missing) specified as the comma-separated pair consisting of 'MissingPlacement' and one of the following:

  • 'auto' — Missing elements are placed last for ascending order and first for descending order.

  • 'first' — Missing elements are placed first.

  • 'last' — Missing elements are placed last.

Element comparison method, specified as the comma-separated pair consisting of 'ComparisonMethod' and one of the following:

  • 'auto' — Sort A by real(A) when A is real, and sort by abs(A) when A is complex.

  • 'real' — Sort A by real(A) when A is real or complex. If A has elements with equal real parts, then use imag(A) to break ties.

  • 'abs' — Sort A by abs(A) when A is real or complex. If A has elements with equal magnitude, then use angle(A) in the interval (-π,π] to break ties.

Output Arguments

collapse all

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

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

Sort index, returned as a vector, matrix, or multidimensional array. I is 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. The output I is a collection of 1-by-3 row index vectors describing the rearrangement of each row of A.

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.

Tips

  • The sortrows function provides additional flexibility for subsorting over multiple columns of matrix or table inputs.

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

Extended Capabilities

Introduced before R2006a

Was this topic helpful?