semver icon indicating copy to clipboard operation
semver copied to clipboard

Sorting of pre-release versions

Open jhamman opened this issue 5 years ago • 1 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 avatar Mar 16 '20 22:03 jhamman

@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.

mattfarina avatar Apr 15 '20 20:04 mattfarina