A more efficient or compact way to sort strings that contain dates
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Stephen23
2024-5-2
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 个评论
Stephen23
2024-5-2
编辑:Stephen23
2024-5-2
"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:
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!