###手机应用应用版本比较(Java)###

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
/**
 * 版本比较
 * @param version1
 * @param version2
 * @return
 * -1: version1小于version2
 * 1:  version1大于version2
 * 0:  version1等于version2
 */
public static int versionCompare(String version1, String version2) {
    String[] arr1 = version1.split("\\."), arr2 = version2
            .split("\\.");

    int maxLen = Math.max(arr1.length, arr2.length);
    int result=0;

    for (int i = 0; i < maxLen; i++) {
        int i1=0, i2=0;
        char[] c1 = null, c2 = null;
        if (arr1.length > i) {
            if (NumberUtils.isDigits(arr1[i])) {
                i1 = NumberUtils.toInt(arr1[i]);
            } else {
                c1 = arr1[i].toCharArray();
                i1 = Integer.parseInt(c1[0]+"");
            }
        } else {
            i1 = Integer.MIN_VALUE;
        }
        if (arr2.length > i) {
            if (NumberUtils.isDigits(arr2[i])) {
                i2 = NumberUtils.toInt(arr2[i]);
            } else {
                c2 = arr2[i].toCharArray();
                i2 = Integer.parseInt(c2[0]+"");
            }
        } else {
            i2 = Integer.MIN_VALUE;
        }

        if (i1 > i2) {
            result = 1;
        } else if (i1 < i2) {
            result = -1;
        } else {
            if (c1 != null && c2 != null) {
                int c12lenmin = c1.length < c2.length ? c1.length : c2.length;
                for (int j = 0; j < c12lenmin; j++) {
                    if (c1[j]>c2[j]) {
                        result = 1;
                    } else if (c1[j] < c2[j]) {
                        result = -1;
                    } else {
                        result = 0;
                    }
                    if (result != 0) {
                        break;
                    } else {
                        if (c1.length > c2.length) {
                            result = 1;
                        } else if (c1.length < c2.length) {
                            result = -1;
                        }
                    }
                }
            }
            if (c1 == null && c2 !=null) {
                if (i1 > (c2[0]-48)) {
                    result = 1;
                } else if (i1 < (c2[0]-48)){
                    result = -1;
                } else {
                    if (c2.length>1) {
                        result = 1;
                    } else {
                        result = 0;
                    }
                }
            }
            if (c2 == null && c1 !=null) {
                if ((c1[0]-48) > i2) {
                    result = 1;
                } else if ((c1[0]-48) < i2){
                    result = 1;
                } else {
                    if (c1.length>1) {
                        result = -1;
                    } else {
                        result = 0;
                    }
                }
            }
        }
        if (result != 0) {
            break;
        }
    }

    return result;
}

测试代码

1
2
int result = versionCompare("3.0.5beta", "3.0.4");
System.out.println(result);

能够正确得出结果1

算法很繁杂,如果有更好的,请回复。

###手机应用应用版本比较(Ruby)###

-1:version1小于version2;1:version1大于version2;0:version1等于version2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
def versionCompare version1,version2
strArr1 = version1.split('.')
strArr2 = version2.split('.')
maxLen = strArr1.length > strArr2.length ? strArr1.length : strArr2.length
result = 0
for i in 0..maxLen
  begin
    sa = strArr1[i].to_i
  rescue
    sa = 0
  end
  begin
    sb = strArr2[i].to_i
  rescue
    sa = 0
  end
  if sa > sb
    result = 1
  elsif sa < sb
    result = -1
  else
    result = 0
  end
  if result != 0
    break;
  end
end 
return result
end

测试

1
versionCompare('3.0.5beta', '3.0.5')

能够正确得出1