title: Sorting the files using the date from the filenames
body: <p>I have a bunch of files with names</p>
  
  <pre><code>32f245bb02abd408a6f0e997640f7e84d3cdf0e4May 2, 2016.java
  a5c3fc386ee6ff7259f4004128baff8a961ec804May 3, 2016.java
  eaefc7552ab8ce39ea26272f995476869ed91e1aMay 3, 2016.java
  </code></pre>
  
  <p>I want to arrange them with the dates (in the file names). I use regex to extract the date from the file names and sorted them but now I'm stuck at sorting the files according to date. I try renaming the files but dates are repeating and cant use the creation/modified/access dates as all are the same.</p>
  
  <pre><code>File directory2 = new File("path to files");
  
              String newfile="";
                  Set&lt;String&gt; source= new HashSet&lt;String&gt;();  
  
                  ArrayList&lt;String&gt; datestring=new ArrayList&lt;String&gt;();
                  try (Stream&lt;Path&gt; paths = Files.walk(Paths.get(directory2.toString()))) {                   
                       paths.skip(1)
                            .forEach(s-&gt;{source.add(s.toString());});
                      } catch (IOException e) {
                              e.printStackTrace();
                        }
  
                  for(Iterator&lt;String&gt; it=source.iterator(); it.hasNext();) { 
                          newfile = it.next().toString();
                          File f = new File(newfile);
                          String Date = f.getName();
  
                          String regex2 = "((?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Sept|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?))(\\s+)(\\d+)(,)(\\s+)((?:(?:[1]{1}\\d{1}\\d{1}\\d{1})|(?:[2]{1}\\d{3})))(?![\\d])";
                          Pattern pattern2 = Pattern.compile(regex2,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
                          Matcher m2 = pattern2.matcher(Date);
                          while(m2.find()) {
                              String file= m2.group(0);
                              datestring.add(file);
                          }
                }
  
                Collections.sort(datestring, new Comparator&lt;String&gt;() {
                          DateFormat f = new SimpleDateFormat("MMM dd, yyyy");
                          @Override
                          public int compare(String o1, String o2) {
                              try {
                                  return f.parse(o1).compareTo(f.parse(o2));
                              } catch (ParseException e) {
                                  throw new IllegalArgumentException(e);
                              }
                          }
                      });     
  
          }
  </code></pre>
  
  <p>I try to rename the files after extracting the date as follows but still if the date is repeating its not possible to rename them and still no luck with sorting them according to the date.</p>
  
  <pre><code>for(Iterator&lt;String&gt; it=source.iterator(); it.hasNext();) { 
                      newfile = it.next().toString();
                      File f = new File(newfile);
                      String Date = f.getName();                      
                      for (String datematch:datestring) {
                          if(Date.contains(datematch)) {
                              f.renameTo(new File("path to new directory"+datematch+".java")); 
  
                          }
                      }
  
  
                  }
  </code></pre>