Skip to content

compareVersion does not properly compare numerical semver -- needs to cast parts to ints #10

@NeilHanlon

Description

@NeilHanlon

// compareVersion takes in two kernel version strings either in the form of "X.Y" or "X.Y.Z" and returns:
// 1 if a > b
// 0 if a == b
// -1 if a < b
func compareVersion(a, b string) int {
// Split version strings into parts
aParts := strings.Split(a, ".")
bParts := strings.Split(b, ".")
// Compare major version
if aParts[0] != bParts[0] {
if aParts[0] > bParts[0] {
return 1
}
return -1
}
// Compare minor version
if aParts[1] != bParts[1] {
if aParts[1] > bParts[1] {
return 1
}
return -1
}
// If there's a third part, compare it
if len(aParts) == 3 && len(bParts) == 3 {
if aParts[2] != bParts[2] {
if aParts[2] > bParts[2] {
return 1
}
return -1
}
}
return 0
}

Expected results

6.1 < 6.12

Actual results

6.1 > 6.12

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions