对包含带数字的字符串的文件名数组进行排序

jus*_*urv 10 java arrays sorting algorithm android

对于我的项目,我需要从FTP服务器下载一个zip文件,该服务器每年发布一个新的zip文件大约13次.我需要按照服务器的命名约定下载最新文件:
Prefix + release number (one or two digits) + year (two digits) + suffix + ".zip"

例如: ALFP1016F.zip

前缀将始终相同(ALFP),后缀为F或P(代表"完整"或"部分";我只需要以后缀F结尾的文件).最重要的是,我需要忽略的目录中还有其他几个文件,因为它们有不同的前缀.然后,我需要按照此优先级顺序获取数组中的最新文件:

  1. 最近一年.当然'99不应该被视为最近一年.
  2. 最新版本号

例如,如果我有这个文件名列表(完整服务器目录):

1stpage712.pdf
1stpage914.pdf
ALFP1015F.zip
ALFP1015P.zip
ALFP716F.zip
ALFP716P.zip
FSFP816F.zip
FSFP816P.zip
Run Code Online (Sandbox Code Playgroud)

我的预期产量是
ALFP716F.zip 因为16是最近的一年,7是该年度的最新版本

.

这是我到目前为止所做的:

//necessary imports
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

//initialize FTP client
ftpClient = new FTPClient();

try {
    //connect to server
    ftpClient.connect(server, port);
    ftpClient.login(username, password);
    ftpClient.enterLocalPassiveMode();
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);

    //list all names from server
    String[] filenames = ftpClient.listNames();

    //return expected file name
    String expectedFileName = returnMostRecent(filenames);
} catch (Exception e) { 
    e.printStackTrace(); 
} finally {
    try {
        if (ftpClient.isConnected()) {
            ftpClient.logout();
            ftpClient.disconnect();
            System.out.println("Disconnected from server");
        }
    } catch (IOException ex) { ex.printStackTrace(); }
}
Run Code Online (Sandbox Code Playgroud)

我在编写这个returnMostRecent(String[])方法时做了一个悲惨的尝试,但结果是一个难以理解的混乱不值得在这里发布.

如何对此数组进行排序并按照优先级顺序有效地返回最新文件?

Ada*_*ker 5

如果您使用Java8,您可以:

String file = Arrays.stream(filenames)
                    .filter(s -> s.startsWith("ALFP") && s.endsWith("F.zip"))
                    .max(getReleaseComparator())                        
                    .orElse(null);
Run Code Online (Sandbox Code Playgroud)

释放比较器基于从文件名中提取数字并进行比较


Til*_*ill 3

我还没有测试过这个,但我认为它应该有效。

private String returnMostRecent(String[] fileNames) {
   String file = null;
   double version = -1;
   for(String name : listNames)
   {
      // skip files that don't match
      if (!name.matches("ALFP[0-9]*F.zip"))
          continue;
      // get digits only
      String digits = name.replaceAll("\\D+","");
      // format digits to <year>.<version>
      String vstr = digits.substring(digits.length-2,digits.length()) + ".";
      if (digits.length() < 4)
         vstr += "0";
      vstr = digits.substring(0, digits.length()-2);
      double v = Double.parseDouble(vstr);
      if (v > version)
      {
          version = v;
          file = name;
      }
   }

   return file;
}
Run Code Online (Sandbox Code Playgroud)