Accelerating the pace of engineering and science

# Documentation Center

• Trial Software

# sort

Sort array elements

## Description

example

B = sort(A) sorts the elements of A in ascending order along the first array dimension whose size does not equal 1. For strings, this is a sort in ASCII dictionary order. The sort is case-sensitive; uppercase letters appear in the output before lowercase.

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

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

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

• If A is a multidimensional array, then sort(A) treats as vectors all values along the first array dimension whose size does not equal 1, and then sorts each vector.

example

B = sort(A,dim) sorts the 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) sorts in the order specified by mode using any of the above syntaxes. The single string, 'ascend', indicates ascending order (default) and 'descend' indicates descending order.

example

[B,I] = sort(___) also returns a collection of index vectors in an array, I, using any of the above syntaxes. I is the same size as A and describes the rearrangement of the elements along the sorted dimension. In general, if B is an array with N dimensions whose sizes do not equal 1, then N-1 loops are required to use the indexing array, I. This requirement is because I contains only location information for the single dimension being sorted. For example,

• If you sort a numeric vector or cell array of strings, then B = A(I).

• If you sort the columns of a matrix , then the above relation holds for each column independently. That is, each column of I is a permutation vector of the corresponding column of A such that

```for j = 1:size(A,2)
B(:,j) = A(I(:,j),j);
end```

## Examples

expand all

### Sort Vector Elements

Create a row vector of random integers.

```rng(0)
A = randi(10,1,10)
```
```A =

9    10     2    10     7     1     3     6    10    10```

Sort the vector elements in ascending order.

`B = sort(A)`
```B =

1     2     3     6     7     9    10    10    10    10```

The result, B, lists the integers from smallest to largest.

Alternatively, use the mode input to sort the vector in descending order.

`C = sort(A,'descend')`
```C =

10    10    10    10     9     7     6     3     2     1```

The result, C, lists the integers from largest to smallest.

### Sort Matrix Columns and Rows

Create a 5-by-5 matrix of random integers.

```rng(1);
A = randi(100,5,5)```
```A =

42    10    42    68    81
73    19    69    42    97
1    35    21    56    32
31    40    88    15    70
15    54     3    20    88
```

Sort the columns of A.

`B = sort(A)`
```B =

1    10     3    15    32
15    19    21    20    70
31    35    42    42    81
42    40    69    56    88
73    54    88    68    97```

The result independently orders each column in ascending (smallest to largest) order.

Sort the rows of A in descending order using the dim and mode inputs.

`B1 = sort(A,2,'descend')`
```B1 =

81    68    42    42    10
97    73    69    42    19
56    35    32    21     1
88    70    40    31    15
88    54    20    15     3```

The result independently orders each row in descending (largest to smallest) order.

### Sort Complex Matrix Elements

Create a 4-by-2 matrix of complex numbers.

`A = [1+1i 2-3i; -1-1i 4+3i; -1+1i 3+2i; 1-1i -2+3i]`
```A =

1.0000 + 1.0000i   2.0000 - 3.0000i
-1.0000 - 1.0000i   4.0000 + 3.0000i
-1.0000 + 1.0000i   3.0000 + 2.0000i
1.0000 - 1.0000i  -2.0000 + 3.0000i```

Sort the columns of A in ascending order.

```B = sort(A)
```
```B =

-1.0000 - 1.0000i   2.0000 - 3.0000i
1.0000 - 1.0000i   3.0000 + 2.0000i
1.0000 + 1.0000i  -2.0000 + 3.0000i
-1.0000 + 1.0000i   4.0000 + 3.0000i```

The sort function sorts complex elements first by absolute value (magnitude), then by phase angle.

Find the absolute values of the complex elements in A.

`C = abs(A)`
```C =

1.4142    3.6056
1.4142    5.0000
1.4142    3.6056
1.4142    3.6056
```

In this case, many of the elements in each column of A have equal magnitudes, so the value of angle largely determines the sorting order.

Evaluate the phase angles of each element in A.

`P = angle(A)`
```P =

0.7854   -0.9828
-2.3562    0.6435
2.3562    0.5880
-0.7854    2.1588```

The values in P determine the sort order for the column elements in A with equal magnitude in C.

### Sort Cell Array of Strings

Create a 3-by-3 cell array of strings.

`A = {'Mud' 'Advent' 'Chair'; 'Truth' 'Jester' 'Fall'; 'Create' 'Double' 'Hyper'}`
```A =

'Truth'     'Jester'    'Fall'
'Create'    'Double'    'Hyper'```

Sort the strings in the cell array in alphabetical order using sort.

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

'Chair'
'Create'
'Double'
'Fall'
'Hyper'
'Jester'
'Mud'
'Truth'

I =

4
7
3
6
8
9
5
1
2```

The result, B, is a 9-by-1 cell array with the strings in alphabetical order.

Use the index vector, I, to obtain B directly from A.

`A(I)`
```ans =

'Chair'
'Create'
'Double'
'Fall'
'Hyper'
'Jester'
'Mud'
'Truth'```

The result is the same as B because I describes the rearrangement of strings such that A(I) = B.

### Sort Multidimensional Array

Create a 2-by-2-by-3 multidimensional array of random integers.

```rng(2);
A = randi(100,2,2,3)```
```A(:,:,1) =

44    55
3    44

A(:,:,2) =

43    21
34    62

A(:,:,3) =

30    63
27    53```

Sort the columns on each page of the array.

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

3    44
44    55

B(:,:,2) =

34    21
43    62

B(:,:,3) =

27    53
30    63```

The result orders the values in each column from smallest to largest.

Sort the array in the third (dim = 3) dimension.

```[C,I] = sort(A,3)
```
```C(:,:,1) =

30    21
3    44

C(:,:,2) =

43    55
27    53

C(:,:,3) =

44    63
34    62

I(:,:,1) =

3     2
1     1

I(:,:,2) =

2     1
3     3

I(:,:,3) =

1     3
2     2
```

The result, C, orders the corresponding elements on each page of A from smallest to largest. For example, C(1,1,1) < C(1,1,2) < C(1,1,3). The corresponding elements on each page of I form 1-by-1-by-3 permutation vectors describing the rearrangement of the elements in A along the third dimension.

Verify the use of the index array with the original array, A.

```for i = 1:size(A,1)
for j = 1:size(A,2)
D(i,j,:) = A(i,j,I(i,j,:));
end
end
isequal(D,C)```
```ans =

1```

The result of logical 1 (true) confirms the validity of the index array, I. Since the permutation vectors are 3-D, two loops are required to use the indexing array.

## Input Arguments

expand all

### A — Input arrayvector | matrix | multidimensional array

Input array, specified as a vector, matrix, or multidimensional array. The data type of A can be numeric, logical, char, cell, or categorical. If A contains NaN values or undefined categorical elements, sort(A) places them on the high end of the sort (as if they are large numbers).

If A is complex, the sort function sorts the elements by magnitude. If magnitudes are equal, the sort function also sorts by phase angle on the interval [−π, π].

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. You cannot use the dim or mode inputs with a cell array.

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).

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

Sorting mode, specified as 'ascend' (default) or 'descend'. You cannot use the dim or mode options with a cell array.

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

Data Types: char

## Output Arguments

expand all

### B — Sorted arrayarray

Sorted array, returned as an array of the same size and class as A. The ordering of the elements in B preserves the order of any equal elements in A.

### I — Sort indexcollection of index vectors

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

If A has repeated elements of equal value, the indices in I preserve the original ordering.