A more efficient or compact way to sort strings that contain dates
1 view (last 30 days)
Show older comments
I have strings that contain dates.
Those strings are in a "random" order, i.e. they are not ordered by following the dates, from 2024/03/01 to 2024/03/31 (i.e. from the 1st of March 2024 to the 31st of March 2024).
Is there a more efficient or compact way to sort the following strings containing dates?
% (1) input (strings containing dates, in a "random" order)
a(1,:) = '123_abc_01_202403020000_202403022359.txt';
a(2,:) = '123_abc_01_202403040000_202403042359.txt';
a(3,:) = '123_abc_01_202403030000_202403032359.txt';
a(4,:) = '123_abc_01_202403050000_202403052359.txt';
a(5,:) = '123_abc_01_202403010000_202403012359.txt';
a
% (2) create substrings with ordered dates, that we can use to compare with the unordered strings of the input
for i = 1 : 31
tmp = [];
if i <=10
tmp = sprintf('%02d',i);
else
tmp = sprintf('%0d',i);
end
b(i,:) = append('_202403',tmp);
end
% sort the unordered strings of the input, by following the substrings that have ordered dates
for i = 1 : 5
for j = 1 : 31
if contains(a(i,:),b(j,:))
which_j(i) = j;
end
end
end
sorted_a = sort(a(which_j,:))
0 Comments
Accepted Answer
Stephen23
on 2 May 2024
a = [...
'123_abc_01_202403020000_202403022359.txt';
'123_abc_01_202403040000_202403042359.txt';
'123_abc_01_202403030000_202403032359.txt';
'123_abc_01_202403050000_202403052359.txt';
'123_abc_01_202403010000_202403012359.txt'];
b = sortrows(a)
Is there are particular reason why you are using a character matrix?
3 Comments
Stephen23
on 2 May 2024
Edited: Stephen23
on 2 May 2024
"How is it possible that sortrows recognises dates inside the strings??"
It doesn't.
"Is there any magic?"
Not really: as long as the dates are written in order from largest unit to smallest unit (i.e. years, months, ... seconds) and use leading zeros to ensure a constant width then a basic character sort will return the dates in chronological order. If those conditions are not met then a character sort will not work, i.e. you will need to parse the dates first.
This is exactly why ISO 8601 specifies timestamps with units going from largest to smallest, and a fixed width:
See also:
More Answers (0)
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!