timetable.sortrows
timetable: ttB = sortrows (ttA)
timetable: ttB = sortrows (ttA, rowDimName)
timetable: ttB = sortrows (ttA, vars)
timetable: ttB = sortrows (ttA, …, direction)
timetable: ttB = sortrows (…, Name, Value)
timetable: [ttB, index] = sortrows (…)
Sort the rows of a timetable.
ttB = sortrows (ttA) orders the rows by their row
times, earliest first. Rows sharing a time keep the order they were
in: the row times alone decide, and the variables are never consulted
to break a tie.
ttB = sortrows (ttA, rowDimName) does the
same, naming the row times through the first of
ttA.Properties.DimensionNames. That name is the only way
to reach them: a numeric index counts the variables, so
sortrows (ttA, 1) orders by the first variable and not by
the times, and there is no index that means the row times. Renaming
the row dimension renames the key with it.
ttB = sortrows (ttA, vars) orders by one or
more variables, named, indexed by number, selected by a logical vector
or picked out by a vartype. A negative index sorts that
variable in descending order. The row dimension name may appear among
vars, in which case the row times take their turn as a key like
any other.
ttB = sortrows (…, direction) sorts as
'ascend' or 'descend', either one direction for every
key or one per key. A direction must follow the keys it applies to,
so sortrows (ttA, 'descend') is an error rather than a
reversed sort: with nothing else given, the first argument is read as
the variables to sort by.
The name-value pair 'MissingPlacement' takes
'auto' (the default), 'first' or 'last' and
says where missing keys go. 'auto' puts them last when
sorting up and first when sorting down. 'ComparisonMethod'
takes 'auto', 'real' or 'abs' and applies to
numeric variables. Both must follow the keys as well.
[ttB, index] = sortrows (…) also returns the
permutation, so that ttA(index,:) is ttB.
The time step is read afresh from the sorted row times rather than carried over, so sorting an out-of-order hourly timetable makes it regular again, and sorting one that ran backwards turns its step from minus one hour to plus one.
See also: issortedrows, timetable
Source Code: timetable
sortrows on a timetable orders by the row times unless told otherwise. Rows sharing a time keep the order they were in: the times alone decide, and the variables are never consulted to break a tie.
t0 = datetime (2024, 1, 1); Reading = [9.4; 3.1; 1.7; 7.2]; TT = timetable (Reading, 'RowTimes', t0 + hours ([1 0 1 0])')
TT =
4x1 timetable
Time Reading
____________________ _______
01-Jan-2024 01:00:00 9.4
01-Jan-2024 00:00:00 3.1
01-Jan-2024 01:00:00 1.7
01-Jan-2024 00:00:00 7.2
sortrows (TT)
ans =
4x1 timetable
Time Reading
____________________ _______
01-Jan-2024 00:00:00 3.1
01-Jan-2024 00:00:00 7.2
01-Jan-2024 01:00:00 9.4
01-Jan-2024 01:00:00 1.7
Name the row dimension to sort by the times explicitly, or a variable to sort by that instead, in which case the row times travel with their rows.
t0 = datetime (2024, 1, 1);
Depth = [12; 45; 23; 8];
Site = categorical ({'N'; 'S'; 'N'; 'S'});
TT = timetable (Depth, Site, 'RowTimes', t0 + hours ([2 0 3 1])')
TT =
4x2 timetable
Time Depth Site
____________________ _____ ____
01-Jan-2024 02:00:00 12 N
01-Jan-2024 00:00:00 45 S
01-Jan-2024 03:00:00 23 N
01-Jan-2024 01:00:00 8 S
sortrows (TT, 'Time', 'descend')
ans =
4x2 timetable
Time Depth Site
____________________ _____ ____
01-Jan-2024 03:00:00 23 N
01-Jan-2024 02:00:00 12 N
01-Jan-2024 01:00:00 8 S
01-Jan-2024 00:00:00 45 S
A numeric index counts the variables, so 1 means Depth, never the times, and a negative index sorts that variable downwards.
sortrows (TT, -1)
ans =
4x2 timetable
Time Depth Site
____________________ _____ ____
01-Jan-2024 00:00:00 45 S
01-Jan-2024 03:00:00 23 N
01-Jan-2024 02:00:00 12 N
01-Jan-2024 01:00:00 8 S
The time step is read afresh from the sorted row times rather than carried over. Sorting an out-of-order hourly timetable makes it regular again, and sorting one that ran backwards turns its step positive.
t0 = datetime (2024, 1, 1); U = timetable ([30; 10; 40; 20], 'RowTimes', t0 + hours ([2 0 3 1])'); U.Properties.TimeStep
ans = duration NaN sec
S = sortrows (U); S.Properties.TimeStep
ans = duration 01:00:00
The same timetable built backwards steps by minus one hour.
Z = timetable ((1:4)', 'RowTimes', t0 - hours (0:3)'); Z.Properties.TimeStep
ans = duration -01:00:00
sortrows (Z).Properties.TimeStep
ans = duration 01:00:00