A more efficient or compact way to sort strings that contain dates

조회 수: 1 (최근 30일)
Sim
Sim 2024년 5월 2일
댓글: Sim 2024년 5월 2일
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
a = 5x40 char array
'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'
% (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,:))
sorted_a = 5x40 char array
'123_abc_01_202403010000_202403012359.txt' '123_abc_01_202403020000_202403022359.txt' '123_abc_01_202403030000_202403032359.txt' '123_abc_01_202403040000_202403042359.txt' '123_abc_01_202403050000_202403052359.txt'

채택된 답변

Stephen23
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)
b = 5x40 char array
'123_abc_01_202403010000_202403012359.txt' '123_abc_01_202403020000_202403022359.txt' '123_abc_01_202403030000_202403032359.txt' '123_abc_01_202403040000_202403042359.txt' '123_abc_01_202403050000_202403052359.txt'
Is there are particular reason why you are using a character matrix?
  댓글 수: 3
Stephen23
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:
Sim
Sim 2024년 5월 2일
Thanks a lot about all these info! Good to know as a good practice for coding and saving files :-)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by