title: How to sort file names in ascending order?
body: <p>I have a set of files in a folder, and all of them starting with a similar name, except one. Here is an example:</p>
  
  <pre><code>Coordinate.txt
  Spectrum_1.txt
  Spectrum_2.txt
  Spectrum_3.txt
  .
  .
  .
  Spectrum_11235
  </code></pre>
  
  <p>I am able to list all the files from the specified folder, but the list is not in an ascending order of the spectrum number. Example: I get the following result when the program is executed:</p>
  
  <pre><code>Spectrum_999.txt
  Spectrum_9990.txt
  Spectrum_9991.txt
  Spectrum_9992.txt
  Spectrum_9993.txt
  Spectrum_9994.txt
  Spectrum_9995.txt
  Spectrum_9996.txt
  Spectrum_9997.txt
  Spectrum_9998.txt
  Spectrum_9999.txt
  </code></pre>
  
  <p>But this order is not correct. There should be Spectrum_1000.txt file after Spectrum_999.txt. Can anyone help? Here is the code:</p>
  
  <pre><code>import java.io.*;
  import java.util.Arrays;
  import java.util.Comparator;
  import java.util.Scanner;
  
      public class FileInput {
  
          public void userInput()
          {
              Scanner scanner = new Scanner( System.in );
              System.out.println("Enter the file path: ");
              String dirPath = scanner.nextLine(); // Takes the directory path as the user input
  
              File folder = new File(dirPath);
              if(folder.isDirectory())
              {
                  File[] fileList = folder.listFiles();
  
                  Arrays.sort(fileList);
  
                  System.out.println("\nTotal number of items present in the directory: " + fileList.length );
  
  
                  // Lists only files since we have applied file filter
                  for(File file:fileList)
                  {
                      System.out.println(file.getName());
                  }
  
                  // Creating a filter to return only files.
                  FileFilter fileFilter = new FileFilter()
                  {
                      @Override
                      public boolean accept(File file) {
                          return !file.isDirectory();
                      }
                  };
  
                  fileList = folder.listFiles(fileFilter);
  
                  // Sort files by name
                  Arrays.sort(fileList, new Comparator()
                  {
                      @Override
                      public int compare(Object f1, Object f2) {
                          return ((File) f1).getName().compareTo(((File) f2).getName());
                      }
                  });
  
                  //Prints the files in file name ascending order
                  for(File file:fileList)
                  {
                      System.out.println(file.getName());
                  }
  
              }   
          }
      }
  </code></pre>