如何使用js比较软件版本号?(只有数字) [英] How to compare software version number using js? (only number)

查看:26
本文介绍了如何使用js比较软件版本号?(只有数字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是软件版本号:

"1.0", "1.0.1", "2.0", "2.0.0.1", "2.0.1"

我怎么能比较这个??假设正确的顺序是:

How can I compare this?? Assume the correct order is:

"1.0", "1.0.1", "2.0", "2.0.0.1", "2.0.1"

这个想法很简单...:读取第一个数字,然后是第二个,然后是第三个......但我无法将版本号转换为浮点数....您还可以像这样看到版本号:

The idea is simple...: Read the first digit, than, the second, after that the third.... But I can't convert the version number to float number.... You also can see the version number like this:

"1.0.0.0", "1.0.1.0", "2.0.0.0", "2.0.0.1", "2.0.1.0"

这更清楚地看到背后的想法是什么......但是,如何将其转换为计算机程序??有没有人知道如何对此进行排序?谢谢你.

and this is more clear to see what is the idea behind... But, how to convert it into a computer program?? Do any one have any idea on how to sorting this? Thank you.

推荐答案

进行这种比较的基本思想是使用 Array.split 从输入字符串中获取部分数组,然后进行比较来自两个数组的成对部分;如果部分不相等,我们知道哪个版本更小.

The basic idea to make this comparison would be to use Array.split to get arrays of parts from the input strings and then compare pairs of parts from the two arrays; if the parts are not equal we know which version is smaller.

有一些重要的细节需要牢记:

There are a few of important details to keep in mind:

  1. 应该如何比较每对中的零件?这个问题想用数字进行比较,但如果我们有不只由数字组成的版本字符串(例如1.0a")怎么办?
  2. 如果一个版本字符串的部分比另一个多,会发生什么情况?很可能1.0"应该被认为小于1.0.1",但是1.0.0"呢?

以下是您可以直接使用的实现代码(带有文档的要点):

Here's the code for an implementation that you can use directly (gist with documentation):

function versionCompare(v1, v2, options) {
    var lexicographical = options && options.lexicographical,
        zeroExtend = options && options.zeroExtend,
        v1parts = v1.split('.'),
        v2parts = v2.split('.');

    function isValidPart(x) {
        return (lexicographical ? /^d+[A-Za-z]*$/ : /^d+$/).test(x);
    }

    if (!v1parts.every(isValidPart) || !v2parts.every(isValidPart)) {
        return NaN;
    }

    if (zeroExtend) {
        while (v1parts.length < v2parts.length) v1parts.push("0");
        while (v2parts.length < v1parts.length) v2parts.push("0");
    }

    if (!lexicographical) {
        v1parts = v1parts.map(Number);
        v2parts = v2parts.map(Number);
    }

    for (var i = 0; i < v1parts.length; ++i) {
        if (v2parts.length == i) {
            return 1;
        }

        if (v1parts[i] == v2parts[i]) {
            continue;
        }
        else if (v1parts[i] > v2parts[i]) {
            return 1;
        }
        else {
            return -1;
        }
    }

    if (v1parts.length != v2parts.length) {
        return -1;
    }

    return 0;
}

这个版本比较部分自然,不接受字符后缀,认为1.7"小于1.7".0".比较模式可以更改为字典序,并且可以使用可选的第三个参数自动填充更短的版本字符串.

This version compares parts naturally, does not accept character suffixes and considers "1.7" to be smaller than "1.7.0". The comparison mode can be changed to lexicographical and shorter version strings can be automatically zero-padded using the optional third argument.

此处有一个运行单元测试"的 JSFiddle;它是 ripper234 的工作(谢谢).

There is a JSFiddle that runs "unit tests" here; it is a slightly expanded version of ripper234's work (thank you).

重要说明:此代码使用 Array.mapArray.every,这意味着它不会在 IE 9 之前的版本中运行.如果你需要支持那些你必须提供 polyfills对于缺失的方法.

Important note: This code uses Array.map and Array.every, which means that it will not run in IE versions earlier than 9. If you need to support those you will have to provide polyfills for the missing methods.

这篇关于如何使用js比较软件版本号?(只有数字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆