[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to sort the files based on the date?

On 7/15/19 2:59 AM, Madhavan Bomidi wrote:
> I am using the following command line to sort the files:
> import glob
> a = sorted(glob.glob('3RIMG_*.h5')
> Following is the result:
> 3RIMG_01APR2018_0514_L2G_AOD.h5
> 3RIMG_01APR2018_0544_L2G_AOD.h5
> 3RIMG_01APR2018_0644_L2G_AOD.h5
> 3RIMG_01APR2018_0714_L2G_AOD.h5
> 3RIMG_01APR2018_0744_L2G_AOD.h5
> 3RIMG_01MAY2018_0515_L2G_AOD.h5
> 3RIMG_01MAY2018_0545_L2G_AOD.h5
> 3RIMG_01MAY2018_0615_L2G_AOD.h5
> 3RIMG_01MAY2018_0645_L2G_AOD.h5
> 3RIMG_01MAY2018_0715_L2G_AOD.h5
> 3RIMG_01MAY2018_0745_L2G_AOD.h5
> 3RIMG_01MAY2018_0815_L2G_AOD.h5.
> 3RIMG_02APR2018_0514_L2G_AOD.h5
> 3RIMG_02APR2018_0544_L2G_AOD.h5
> 3RIMG_02APR2018_0614_L2G_AOD.h5
> 3RIMG_02APR2018_0644_L2G_AOD.h5
> 3RIMG_02APR2018_0714_L2G_AOD.h5
> 3RIMG_02APR2018_0744_L2G_AOD.h5
> 3RIMG_02APR2018_0814_L2G_AOD.h5
> 3RIMG_02MAY2018_0515_L2G_AOD.h5
> 3RIMG_02MAY2018_0545_L2G_AOD.h5
> 3RIMG_02MAY2018_0615_L2G_AOD.h5
> 3RIMG_02MAY2018_0645_L2G_AOD.h5
> 3RIMG_02MAY2018_0715_L2G_AOD.h5
> 3RIMG_02MAY2018_0745_L2G_AOD.h5
> 3RIMG_02MAY2018_0815_L2G_AOD.h5
> 3RIMG_31MAR2018_0514_L2G_AOD.h5
> 3RIMG_31MAR2018_0544_L2G_AOD.h5
> 3RIMG_31MAR2018_0614_L2G_AOD.h5
> 3RIMG_31MAR2018_0644_L2G_AOD.h5
> 3RIMG_31MAR2018_0714_L2G_AOD.h5
> 3RIMG_31MAR2018_0744_L2G_AOD.h5
> As you can see from the list of files displayed after sorting, the files are not arranged according to the increasing order of the date since the date is in the DDMMMYYYY format. 
> Can anyone suggest me how I can sort theses files in increasing order of the date on the file name?
> Thanks in advance

One quick comment which may or may not be applicable, as it relies on
changing the file name format would be to switch the file names to using
something like an ISO date format fo the name, ISO dates are in the
format YYYY-MM-DD (Month is a two digit number) which has the nice
attribute that the alphabetical sort is also a date sort.

If you can't do this, then as others have pointed out, you need to parse
out the date from the filename, and use that as the sort key.

Richard Damon