title: Ordering files numerically based on the middle of the character string in r
body: <p>I have a list of .tif imagery in a directory, which I loop through to do some raster math later on. I would like the .tif files to be ordered by date, so that when the loop is run I know which date corresponds to which .tif file based on its order in the list. I think I need to either rename the files, or figure out some way to sort them after the loop.</p>
  <p>For example:</p>
  <pre><code>rastlist &lt;- list.files(here::here(&quot;data&quot;, &quot;s2_cropped&quot;), 
                         pattern = '.tif$', all.files = TRUE, full.names = TRUE)
  rastlist
  [1] &quot;/data/s2_cropped/S2A2A_20200307_135_36MYD_BOA_10.tif&quot;
  [2] &quot;/data/s2_cropped/S2A2A_20200605_135_36MYD_BOA_10.tif&quot; 
  [3] &quot;/data/s2_cropped/S2A2A_20200705_135_36MYD_BOA_10.tif&quot; 
  [4] &quot;/data/s2_cropped/S2A2A_20200804_135_36MYD_BOA_10.tif&quot; 
  [5] &quot;/data/s2_cropped/S2A2A_20200903_135_36MYD_BOA_10.tif&quot; 
  [6] &quot;/data/s2_cropped/S2A2A_20201003_135_36MYD_BOA_10.tif&quot; 
  [7] &quot;/data/s2_cropped/S2A2A_20201102_135_36MYD_BOA_10.tif&quot; 
  [8] &quot;/data/s2_cropped/S2A2A_20201202_135_36MYD_BOA_10.tif&quot; 
  [9] &quot;/data/s2_cropped/S2B2A_20200102_135_36MYD_BOA_10.tif&quot; 
  [10] &quot;/data/s2_cropped/S2B2A_20200211_135_36MYD_BOA_10.tif&quot; 
  [11] &quot;/data/s2_cropped/S2B2A_20200401_135_36MYD_BOA_10.tif&quot; 
  [12] &quot;/data/s2_cropped/S2B2A_20200501_135_36MYD_BOA_10.tif&quot; 
  </code></pre>
  <p>I think it's ordering alphabetically based on the first part of the string &quot;S2A2A&quot; &amp; &quot;S2B2A&quot;, but I want it ordered by the date like this:</p>
  <pre><code>rastlist
  [1] &quot;/data/s2_cropped/S2B2A_20200102_135_36MYD_BOA_10.tif&quot; 
  [2] &quot;/data/s2_cropped/S2B2A_20200211_135_36MYD_BOA_10.tif&quot;
  [3] &quot;/data/s2_cropped/S2A2A_20200307_135_36MYD_BOA_10.tif&quot;
  [4] &quot;/data/s2_cropped/S2B2A_20200401_135_36MYD_BOA_10.tif&quot; 
  [5] &quot;/data/s2_cropped/S2B2A_20200501_135_36MYD_BOA_10.tif&quot;
  [6] &quot;/data/s2_cropped/S2A2A_20200605_135_36MYD_BOA_10.tif&quot; 
  [7] &quot;/data/s2_cropped/S2A2A_20200705_135_36MYD_BOA_10.tif&quot; 
  [8] &quot;/data/s2_cropped/S2A2A_20200804_135_36MYD_BOA_10.tif&quot; 
  [9] &quot;/data/s2_cropped/S2A2A_20200903_135_36MYD_BOA_10.tif&quot; 
  [10] &quot;/data/s2_cropped/S2A2A_20201003_135_36MYD_BOA_10.tif&quot; 
  [11] &quot;/data/s2_cropped/S2A2A_20201102_135_36MYD_BOA_10.tif&quot; 
  [12] &quot;/data/s2_cropped/S2A2A_20201202_135_36MYD_BOA_10.tif&quot; 
  </code></pre>
  <p>I'm then looping through to do some raster math on each stack. I don't know if this part of the code matters too much but it looks something like this.</p>
  <pre><code>ndvi_list &lt;- list() #empty vector to store ndvi values
  
  for  (i in 1:length(rastlist)) {
  
  b &lt;- brick(rastlist[[i]]
  ndvi_list[i] &lt;- (b[[4]] - b[[1]]) / (b[[4]] + b[[1]])
  
  }
  
  The result of the loop is 12 rasters in a list
  </code></pre>