Azzera filtri
Azzera filtri

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

2 visualizzazioni (ultimi 30 giorni)
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 = 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);
tmp = sprintf('%0d',i);
b(i,:) = append('_202403',tmp);
% 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;
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'

Risposta accettata

Stephen23 il 2 Mag 2024
a = [...
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 Commenti
Stephen23 il 2 Mag 2024
Modificato: Stephen23 il 2 Mag 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 il 2 Mag 2024
Thanks a lot about all these info! Good to know as a good practice for coding and saving files :-)

Accedi per commentare.

Più risposte (0)


Scopri di più su Time Series Objects in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by