English 中文(简体)
Java 排序算法( 转换为字符串) [关闭]
原标题:Java sorting algorithm (version as string) [closed]

假设我有一个版本号列表 作为字符串

  • 2_1.1.1_release
  • 3-2_snapshot
  • 3.2.1 <---the one I want
  • 1.3(stable)
  • 0.2(1_releaseCan)

each version is a String . I have split them by regular expression, save them into a collection of String[]s collection how do I design a algorithm to find the biggest version number? NOTE 3.2 is less then 3.2.1 because 3.2 is equals to 3.2.0 yes any missing digit is seeing as 0

感谢大家感谢

最佳回答

尝试此比较器 :

private static final Comparator<String[]> comp = new Comparator<String[]>() {
    public int compare(String[] arg0, String[] arg1) {
        int length = arg0.length;
        if (arg1.length>arg0.length) length = arg1.length;

        for (int i=0; i<length; i++) {
            String s0 = null;
            if (i<arg0.length) s0 = arg0[i];
            Integer i0 = (s0==null)?0:Integer.parseInt(s0);
            String s1 = null;
            if (i<arg1.length) s1 = arg1[i];
            Integer i1 = (s1==null)?0:Integer.parseInt(s1);
            if (i0.compareTo(i1)<0) return -1;
            else if (i1.compareTo(i0)<0) return 1;
        }
        return 0;
    }
};

然后使用数组. sort () 方法: 数组. sort( 字符串、 comb) ;

问题回答

虽然不是最佳解决办法,但直截了当的解决办法

public class VersionComparator implements Comparator<String>  {

  public int compare(String version1, String version2) {
    // Split version into parts
    String parts1[] = getVersionParts(version1),
           parts2[] = getVersionParts(version2);

    // Go through common prefix left to right, first part which is higher indicates
    // higher version (4.2.1 > 4.2.0 > 3.9.9)
    for (int i = 0 ; i < Math.min(parts1.length, parts2.length); i++) {
      int partComparison = compareVersionPart(parts1[i], parts2[i]);
      if (partComparison != 0){
        return partComparison;
      }
    }

    // Common prefix is the same; longer value means higher version
    // (3.2.1 > 3.2)
    if (parts1.length > parts2.length) {
      return 1;
    } else if (parts1.length < parts2.length) {
      return -1;
    } else {
      return 0;
    }

  }

  protected String[] getVersionParts(String version) {
    return version.split("\.");
  }

  protected int compareVersionPart(String part1, String part2) {
    int versionPart1 = Integer.parseInt(part1),
        versionPart2 = Integer.parseInt(part2);

    if (versionPart1 > versionPart2) {
      return 1;
    } else if (versionPart1 < versionPart2) {
      return -1;
    } else {
      return 0;
    }
  }

}

Then you do something like Arrays.sort(versions, new VersionComparator()) and pick last element. UPDATE: Collections.max() would be better alternative.

参照方的算法是这样的:

public int compare(String a, String b) {
    Integer a1 = firstPart(a);
    Integer b1 = firstPart(b);

    int res = a1.compareTo(b1);

    if (res != 0) 
        return res;

    return compare(remainingParts(a), remainingParts(b));
}

您只需执行 firstPart remaining parts 。 您可以使用 arrays.sort





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...