Filter löschen
Filter löschen

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

3 Ansichten (letzte 30 Tage)
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'

Akzeptierte Antwort

Stephen23
Stephen23 am 2 Mai 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)
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 Kommentare
Stephen23
Stephen23 am 2 Mai 2024
Bearbeitet: Stephen23 am 2 Mai 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:
Sim
Sim am 2 Mai 2024
Thanks a lot about all these info! Good to know as a good practice for coding and saving files :-)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Time Series Objects finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by