版本号比较方法

var compareVersion = function(version1, version2) {
    const arr1 = version1.split('.').map(e => e * 1)
    const arr2 = version2.split('.').map(e => e * 1)
    const length = Math.max(arr1.length, arr2.length)
    for (let i = 0; i < length; i ++) {
        if ((arr1[i] || 0) > (arr2[i] || 0)) return 1
        if ((arr1[i] || 0) < (arr2[i] || 0)) return -1
    }
    return 0
};
posted @ 2022-09-02 17:46  不叫一日闲过  阅读(63)  评论(0编辑  收藏  举报