How to sort the cell with string based on dates (earliest date should begin first)

1 vue (au cours des 30 derniers jours)
I want to sort the following cell
{'(Z98.1 Z47.89 Z98.1 Z98.1 10/13/2020), (Z98.1 10/15/2020), (Z98.1 10/9/2020), (Z98.1 11/20/2020)'}
into
{'(Z98.1 10/9/2020), (Z98.1 Z47.89 Z98.1 Z98.1 10/13/2020), (Z98.1 10/15/2020), (Z98.1 11/20/2020)'}
Thank you
  4 commentaires

Connectez-vous pour commenter.

Réponse acceptée

Paul
Paul le 2 Juin 2023
Hi Geerthy,
Here is one approach. Some of the steps can be combined if fewer lines of code are desired.
c = {'(Z98.1 Z47.89 Z98.1 Z98.1 10/13/2020), (Z98.1 10/15/2020), (Z98.1 10/9/2020), (Z98.1 11/20/2020)'};
s = split(c,",")
s = 4×1 cell array
{'(Z98.1 Z47.89 Z98.1 Z98.1 10/13/2020)'} {' (Z98.1 10/15/2020)' } {' (Z98.1 10/9/2020)' } {' (Z98.1 11/20/2020)' }
d = datetime(reverse(extractAfter(extractBefore(reverse(s)," "),1)))
d = 4×1 datetime array
13-Oct-2020 15-Oct-2020 09-Oct-2020 20-Nov-2020
[~,index] = sort(d);
s = s(index);
s = strtrim(s)
s = 4×1 cell array
{'(Z98.1 10/9/2020)' } {'(Z98.1 Z47.89 Z98.1 Z98.1 10/13/2020)'} {'(Z98.1 10/15/2020)' } {'(Z98.1 11/20/2020)' }
s = join(s,", ")
s = 1×1 cell array
{'(Z98.1 10/9/2020), (Z98.1 Z47.89 Z98.1 Z98.1 10/13/2020), (Z98.1 10/15/2020), (Z98.1 11/20/2020)'}
% {'(Z98.1 10/9/2020), (Z98.1 Z47.89 Z98.1 Z98.1 10/13/2020), (Z98.1 10/15/2020), (Z98.1 11/20/2020)'}

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by