24

我有两个字符串(它们实际上是版本号,可以是任何版本号)

a := "1.05.00.0156"  
b := "1.0.221.9289"

我想比较一下哪个更大。在golang中怎么做?

4

12 回答 12

24

Hashicorp 有一个很好的解决方案 - https://github.com/hashicorp/go-version

import github.com/hashicorp/go-version
v1, err := version.NewVersion("1.2")
v2, err := version.NewVersion("1.5+metadata")
// Comparison example. There is also GreaterThan, Equal, and just
// a simple Compare that returns an int allowing easy >=, <=, etc.
if v1.LessThan(v2) {
    fmt.Printf("%s is less than %s", v1, v2)
}
于 2016-06-20T12:13:14.277 回答
13

前段时间我创建了一个版本比较库:https ://github.com/mcuadros/go-version

version.CompareSimple("1.05.00.0156", "1.0.221.9289")
//Returns: 1

好好享受!

于 2013-11-12T22:48:37.747 回答
9

这是一个通用的解决方案。

package main

import "fmt"

func VersionOrdinal(version string) string {
    // ISO/IEC 14651:2011
    const maxByte = 1<<8 - 1
    vo := make([]byte, 0, len(version)+8)
    j := -1
    for i := 0; i < len(version); i++ {
        b := version[i]
        if '0' > b || b > '9' {
            vo = append(vo, b)
            j = -1
            continue
        }
        if j == -1 {
            vo = append(vo, 0x00)
            j = len(vo) - 1
        }
        if vo[j] == 1 && vo[j+1] == '0' {
            vo[j+1] = b
            continue
        }
        if vo[j]+1 > maxByte {
            panic("VersionOrdinal: invalid version")
        }
        vo = append(vo, b)
        vo[j]++
    }
    return string(vo)
}

func main() {
    versions := []struct{ a, b string }{
        {"1.05.00.0156", "1.0.221.9289"},
        // Go versions
        {"1", "1.0.1"},
        {"1.0.1", "1.0.2"},
        {"1.0.2", "1.0.3"},
        {"1.0.3", "1.1"},
        {"1.1", "1.1.1"},
        {"1.1.1", "1.1.2"},
        {"1.1.2", "1.2"},
    }
    for _, version := range versions {
        a, b := VersionOrdinal(version.a), VersionOrdinal(version.b)
        switch {
        case a > b:
            fmt.Println(version.a, ">", version.b)
        case a < b:
            fmt.Println(version.a, "<", version.b)
        case a == b:
            fmt.Println(version.a, "=", version.b)
        }
    }
}

输出:

1.05.00.0156 > 1.0.221.9289
1 < 1.0.1
1.0.1 < 1.0.2
1.0.2 < 1.0.3
1.0.3 < 1.1
1.1 < 1.1.1
1.1.1 < 1.1.2
1.1.2 < 1.2
于 2013-08-23T21:15:49.930 回答
4

go-semver是 Go 的语义版本控制库。它允许您解析和比较两个语义版本字符串。

例子:

vA := semver.New("1.2.3")
vB := semver.New("3.2.1")

fmt.Printf("%s < %s == %t\n", vA, vB, vA.LessThan(*vB))

输出:

1.2.3 < 3.2.1 == 真

于 2018-09-13T04:05:45.260 回答
2

根据杰里米·沃尔的回答:

  func compareVer(a, b string) (ret int) {
            as := strings.Split(a, ".")
            bs := strings.Split(b, ".")
            loopMax := len(bs)
            if len(as) > len(bs) {
                    loopMax = len(as)
            }
            for i := 0; i < loopMax; i++ { 
                    var x, y string
                    if len(as) > i {
                            x = as[i]
                    }
                    if len(bs) > i {
                            y = bs[i]
                    }
                    xi,_ := strconv.Atoi(x)
                    yi,_ := strconv.Atoi(y)
                    if xi > yi {
                            ret = -1
                    } else if xi < yi {
                            ret = 1
                    }
                    if ret != 0 {
                            break
                    }
            }       
            return 
    }

http://play.golang.org/p/AetJqvFc3B

于 2014-11-04T07:23:41.597 回答
1

这取决于你所说的更大的意思。

一种天真的方法是:

package main

import "fmt"
import "strings"

func main() {
    a := strings.Split("1.05.00.0156", ".")
    b := strings.Split("1.0.221.9289", ".")
    for i, s := range a {
        var ai, bi int
        fmt.Sscanf(s, "%d", &ai)
        fmt.Sscanf(b[i], "%d", &bi)
        if ai > bi {
            fmt.Printf("%v is bigger than %v\n", a, b)
            break
        }
        if bi > ai {
            fmt.Printf("%v is bigger than %v\n", b, a)
            break
        }
    }
}

http://play.golang.org/p/j0MtFcn44Z

于 2013-08-23T19:24:35.830 回答
1

力求清晰和简单:

func intVer(v string) (int64, error) {
    sections := strings.Split(v, ".")
    intVerSection := func(v string, n int) string {
        if n < len(sections) {
            return fmt.Sprintf("%04s", sections[n])
        } else {
            return "0000"
        }
    }
    s := ""
    for i := 0; i < 4; i++ {
        s += intVerSection(v, i)
    }
    return strconv.ParseInt(s, 10, 64)
}

func main() {
    a := "3.045.98.0832"
    b := "087.2345"
    va, _ := intVer(a)
    vb, _ := intVer(b)
    fmt.Println(va<vb)
}

比较版本意味着解析,所以我相信这两个步骤应该分开以使其健壮。

于 2015-05-27T19:20:13.217 回答
1

以下是一些用于版本比较的库:

  1. https://github.com/blang/semver
  2. https://github.com/Masterminds/semver
  3. https://github.com/hashicorp/go-version
  4. https://github.com/mcuadros/go-version

我用过blang/semver。例如: https: //play.golang.org/p/1zZvEjLSOAr

 import github.com/blang/semver/v4
 
  v1, err := semver.Make("1.0.0-beta")
  v2, err := semver.Make("2.0.0-beta")
 
  // Options availabe
  v1.Compare(v2)  // Compare
  v1.LT(v2)       // LessThan
  v1.GT(v2)       // GreaterThan
于 2020-08-20T12:20:24.797 回答
0
import (
    "fmt"
    "strconv"
    "strings"
)


func main() {
    j := ll("1.05.00.0156"  ,"1.0.221.9289")
   fmt.Println(j)
}


func ll(a,b string) int {
    var length ,r,l int = 0,0,0
    v1 := strings.Split(a,".")
    v2 := strings.Split(b,".")
    len1, len2 := len(v1), len(v2)

    length = len2
    if len1 > len2 {
       length = len1
    }

    for i:= 0;i<length;i++ {
        if i < len1 && i < len2 {
            if v1[i] == v2[i] {
                continue
            }
        }
        r = 0
        if i < len1 {
            if number, err := strconv.Atoi(v1[i]); err == nil {
                r = number
            }
        }

        l = 0
        if i < len2 {
            if number, err := strconv.Atoi(v2[i]); err == nil {
                l = number
            }
        }

        if r < l {
            return -1
        }else if r> l {
            return 1
        }
    }

    return 0
}
于 2016-03-22T09:02:34.560 回答
0

在 leetcode 中测试:https ://leetcode.com/problems/compare-version-numbers/

func compareVersion(version1 string, version2 string) int {
    len1, len2, i, j := len(version1), len(version2), 0, 0
    for i < len1 || j < len2 {
        n1 := 0
        for i < len1 && '0' <= version1[i] && version1[i] <= '9' {
            n1 = n1 * 10 + int(version1[i] - '0')
            i++
        }
        n2 := 0
        for j < len2 && '0' <= version2[j] && version2[j] <= '9' {
            n2 = n2 * 10 + int(version2[j] - '0')
            j++
        }
        if n1 > n2 {
            return 1
        }
        if n1 < n2 {
            return -1
        }
        i, j = i+1, j+1
    }
    return 0
}
于 2021-11-16T08:50:52.963 回答
0

如果你能保证版本字符串的格式相同(即SemVer),你可以转换成int并比较int。这是对 SemVer 的切片进行排序的实现:

    versions := []string{"1.0.10", "1.0.6", "1.0.9"}
    sort.Slice(versions[:], func(i, j int) bool {
        as := strings.Split(versions[i], ".")
        bs := strings.Split(versions[j], ".")
        if len(as) != len(bs) || len(as) != 3 {
            return versions[i] < versions[j]
        }

        ais := make([]int, len(as))
        bis := make([]int, len(bs))
        for i := range as {
            ais[i], _ = strconv.Atoi(as[i])
            bis[i], _ = strconv.Atoi(bs[i])
        }

        //X.Y.Z

        // If X and Y are the same, compare Z
        if ais[0] == bis[0] && ais[1] == bis[1] {
            return ais[2] < bis[2]
        }

        // If X is same, compare Y
        if ais[0] == bis[0] {
            return ais[1] < bis[1]
        }

        // Compare X
        return ais[0] < bis[0]
    })
    fmt.Println(versions)
于 2021-07-12T16:59:13.283 回答
-8

将“1.05.00.0156”转换为“0001”+“0005”+“0000”+“0156”,然后转换为int64。

将“1.0.221.9289”转换为“0001”+“0000”+“0221”+“9289”,然后转换为int64。

比较两个 int64 值。

试一下Go playground

于 2013-08-23T19:12:36.273 回答