sortrows
Sort rows of matrix or table
Syntax
Description
sorts B
= sortrows(A
,column
)A
based on the columns specified in the vector
column
. For example, sortrows(A,4)
sorts the rows of A
in ascending order based on the elements
in the fourth column. sortrows(A,[4 6])
first sorts the rows
of A
based on the elements in the fourth column, then based
on the elements in the sixth column to break ties.
sorts the rows of B
= sortrows(___,direction
)A
in the order specified by
direction
for any of the previous syntaxes.
direction
can be 'ascend'
(default)
for ascending order or 'descend'
for descending order.
direction
can also be a cell array whose elements are
'ascend'
and 'descend'
, where each
element corresponds to a column that sortrows
operates on.
For example, sortrows(A,[4 6],{'ascend' 'descend'})
sorts the
rows of A
in ascending order based on the fourth column, then
in descending order based on the sixth column to break ties.
specifies additional parameters for sorting rows. For example,
B
= sortrows(___,Name,Value
)sortrows(A,'ComparisonMethod','abs')
sorts the elements
of A
by magnitude.
sorts the rows of a table or timetable.tblB
= sortrows(tblA
)
If
tblA
is a table, thensortrows
sortstblA
in ascending order based on the values in the first variable. If elements in the first variable are repeated, thensortrows
sorts by the elements in the second variable, and so on.If
tblA
is a timetable, thensortrows
sorts the rows oftblA
in ascending order based on its row times. However, the rows are sorted only with respect to the row times. If row times are repeated, thensortrows
does not sort by the elements in the timetable variables.Row times of a timetable are
datetime
orduration
values that label the rows along the first dimension of the timetable.
sorts a table based on its row names. Row names of a table label the rows along
the first dimension of the table. If tblB
= sortrows(tblA
,'RowNames')tblA
does not have row
names, that is, if tblA.Properties.RowNames
is empty, then
sortrows
returns tblA
.
This syntax is not supported when tblA
is a
timetable.
sorts tblB
= sortrows(tblA
,rowDimName
)tblA
by row labels rowDimName
along
the first dimension.
If
tblA
is a table, then row labels are row names.If
tblA
is a timetable, then row labels are row times.
sorts a table by the elements in the variables specified by
tblB
= sortrows(tblA
,vars
)vars
. For example,
sortrows(tblA,{'Var1','Var2'})
first sorts the rows of
tblA
based on the elements in Var1
,
then by the elements in Var2
.
If
tblA
is a table and it has row names, thenvars
can include the row names.If
tblA
is a timetable, thenvars
can include the row times.
sorts tblB
= sortrows(___,direction
)tblA
in the order specified by
direction
for any of the previous table syntaxes.
direction
can be 'ascend'
or
'descend'
, which is applied to all specified variables,
row names, or row times that sortrows
operates on.
direction
can also be a cell array whose elements are
'ascend'
and 'descend'
, where each
element corresponds to the specified variables, row names, or row times being
sorted on.
specifies additional parameters for sorting rows of a table or timetable. For
example, tblB
= sortrows(___,Name,Value
)sortrows(tblA,'Var1','MissingPlacement','first')
sorts based on the elements in Var1
, ordering missing
elements such as NaN
at the beginning of the table.
Examples
Input Arguments
Output Arguments
Extended Capabilities
Version History
Introduced before R2006a
See Also
issortedrows
| sort
| issorted
| topkrows