Sortrow on certain elements within array

I am working on reworking the code which sort the text files into order via the date on which they are labelled.
The way i have been doing is by using the sortrow function and using the elements in the colums from the beginning to the end of the date to sort the rows. The code works in that it displays the files in date order, however it first displays the files from 2020 and then goes on to 2019. The problem is that the files are named as: filename[mm dd yyyy]. My way of tackling the problem is to first sort by year, then month, then date. However i dont know how to hold the positions of the file of the previously sorted set of data. I have tried to use the last element of the date ie '9' and '0' and then apply a descend to the sort with no avail. I've also tried different combinations of date then month then year.
Code
foldercontents = ls;
foldercontents = sortrows(foldercontents,[21:33]);
Results
>> foldercontents = sortrows(foldercontents,[11:23])
foldercontents =
'[10 00 13][01 10 2020].txt '
'[10 17 33][01 14 2020].txt '
'[09 53 15][01 21 2020].txt '
'[09 17 22][01 30 2020].txt '
'[14 35 30][02 11 2020].txt '
'[09 55 38][02 12 2020].txt '
'[14 50 31][03 04 2020].txt '
'[10 55 59][03 17 2020].txt '
'[14 50 49][03 18 2020].txt '
'[08 44 30][03 19 2020].txt '
'[08 27 22][03 23 2020].txt '
'[16 01 18][03 30 2020].txt '
'[09 44 10][04 02 2020].txt '
'[15 23 46][04 06 2020].txt '
'[12 04 17][04 15 2019].txt '
'[13 26 24][04 17 2019].txt '
'[10 05 13][04 18 2019].txt '
'[13 33 32][04 23 2019].txt '
'[16 46 57][04 23 2019].txt '
'[13 47 41][04 30 2019].txt '
'[13 19 54][05 01 2019].txt '
'[17 04 53][05 09 2019].txt '
'[10 38 46][05 13 2019].txt '
'[15 17 31][05 13 2019].txt '
'[10 41 53][05 14 2019].txt '
'[11 01 11][05 15 2019].txt '

Respuestas (2)

Ameer Hamza
Ameer Hamza el 15 de Abr. de 2020
Use it with descend option
foldercontents = sortrows(foldercontents,[11:23], 'descend')
Steven Lord
Steven Lord el 15 de Abr. de 2020

0 votos

You could use sortrows, but another approach would be to turn the date and time information stored in the filenames into a datetime array. Once you have that datetime array, call sort on that array with two output arguments, and use the second output to reorder the array containing your filenames to match the order of the sorted datetime array.

La pregunta está cerrada.

Productos

Versión

R2020a

Preguntada:

el 15 de Abr. de 2020

Cerrada:

el 20 de Ag. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by