The idea would be to use a comparator to compare each String (I removed the part which was using the Set) :
public static void main(String [] args){
String [] names = { "0.0.1.1",
"1.0.1.1",
"0.0.1.2",
"0.0.1.5",
"0.0.1.11",
"0.0.0.100",
"0.0.0.256",
"0.0.2.1",
"0.0.1.68"};
String maxVersion = names[0];
for(int i = 1; i < names.length; i++){
if(compare(maxVersion, names[i]) == 1)
maxVersion = names[i];
}
System.out.print(maxVersion);
}
static int compare(String a, String b){
String[] sA = a.split("\\.");
String[] sB = b.split("\\.");
for(int i = 0; i < sA.length; i++){
int cmp = Integer.compare(Integer.parseInt(sB[i]), Integer.parseInt(sA[i]));
if(cmp != 0)
return cmp;
}
return 0;
}
Which outputs:
"1.0.1.1"
Note that this assume that each version number has the same length, but you can change this by checking both the length of the resulting arrays when splitting.
i got another problem: i got some files next to the Version Folders, so i can't get the names List from File.list()
You can provide a FileNameFilter
with the listFiles
method:
File file = new File("/pathToVersionFolders");
File[] files = file.listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return new File(dir, name).isDirectory();
}
});
Now you have only the directories. If you want to get only the names, you can use list
.