Masterminds / semver

Work with Semantic Versions in Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sorting of pre-release versions

jhamman opened this issue · comments

How are pre-release versions intended to be sorted in this library?

Take this simple test:

package main

import (
	"fmt"
	"github.com/coreos/go-semver/semver"
)

func main() {

	raw := []string{"0.11.0-20.g070e4a9", "0.12.0", "0.11.0"}
	fmt.Println(fmt.Sprintf(`Unsorted Tags::%s`, raw))
	var tags []*semver.Version
	for _, r := range raw {	
		tags = append(tags, semver.New(r))
	}
	semver.Sort(tags)
	fmt.Println(fmt.Sprintf(`Sorted Tags (oldest -> newest)::%s`, tags))
}

which yields:

$ go run test.go
Unsorted Tags::[0.11.0-20.g070e4a9 0.12.0 0.11.0]
Sorted Tags (oldest -> newest)::[0.11.0-20.g070e4a9 0.11.0 0.12.0]

I would have expected the following ordering: [0.11.0 0.11.0-20.g070e4a9 0.12.0]. Is this the expected behavior?

@jhamman pre-releases are those that are before a release. The spec provides an example of precedence which has an ordering in it at https://semver.org/#spec-item-11. The order you see in the sorted tags is the order that is correct per the spec.