lo que tienes que hacer es pasarle al metodo sort() del Array un comparador personalizado. en este caso AlphanumFileComparator
y de esa forma tu código tendría que quedar de la siguiente forma:
import org.apache.commons.io.filefilter.WildcardFileFilter;
import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Scanner;
public class Principal {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Escribe la ruta: ");
String ruta = sc.nextLine();
System.out.println("Escribe la extension: ");
String ext = sc.nextLine();
File carpeta = new File(ruta);
FileFilter fileFilter = new WildcardFileFilter("*." + ext);
if (carpeta.exists()) {
if (carpeta.isDirectory()) {
File[] archivos = carpeta.listFiles(fileFilter);
Arrays.sort(archivos, new AlphanumFileComparator());
for (int i = 0; i < archivos.length; i++) {
System.out.println(archivos[i].getName());
}
}
}
}
}
Este es el código del Comparador:
import java.io.File;
import java.util.Comparator;
public class AlphanumFileComparator implements Comparator {
private final boolean isDigit(char ch) {
return ch >= 48 && ch <= 57;
}
private final String getChunk(String s, int slength, int marker) {
StringBuilder chunk = new StringBuilder();
char c = s.charAt(marker);
chunk.append(c);
marker++;
if (isDigit(c)) {
while (marker < slength) {
c = s.charAt(marker);
if (!isDigit(c))
break;
chunk.append(c);
marker++;
}
} else {
while (marker < slength) {
c = s.charAt(marker);
if (isDigit(c))
break;
chunk.append(c);
marker++;
}
}
return chunk.toString();
}
public int compare(Object o1, Object o2) {
if (!(o1 instanceof File) || !(o2 instanceof File)) {
return 0;
}
File f1 = (File) o1;
File f2 = (File) o2;
String s1 = f1.getName();
String s2 = f2.getName();
int thisMarker = 0;
int thatMarker = 0;
int s1Length = s1.length();
int s2Length = s2.length();
while (thisMarker < s1Length && thatMarker < s2Length) {
String thisChunk = getChunk(s1, s1Length, thisMarker);
thisMarker += thisChunk.length();
String thatChunk = getChunk(s2, s2Length, thatMarker);
thatMarker += thatChunk.length();
/** If both chunks contain numeric characters, sort them numerically **/
int result = 0;
if (isDigit(thisChunk.charAt(0)) && isDigit(thatChunk.charAt(0))) {
// Simple chunk comparison by length.
int thisChunkLength = thisChunk.length();
result = thisChunkLength - thatChunk.length();
// If equal, the first different number counts
if (result == 0) {
for (int i = 0; i < thisChunkLength; i++) {
result = thisChunk.charAt(i) - thatChunk.charAt(i);
if (result != 0) {
return result;
}
}
}
} else {
result = thisChunk.compareTo(thatChunk);
}
if (result != 0)
return result;
}
return s1Length - s2Length;
}
}
De esa forma tu resultado sera algo como:
Escribe la ruta:
/home/efren/Archivos
Escribe la extension:
sql
1.sql
2.sql
3.sql
4.sql
5.sql
6.sql
7.sql
8.sql
9.sql
10.sql
11.sql
12.sql
13.sql
Process finished with exit code 0