You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+32
Original file line number
Diff line number
Diff line change
@@ -95,3 +95,35 @@ The `Version` and `RelaxedVersion` provides optimized `MarshalBinary`/`Unmarshal
95
95
## Yaml parsable with `gopkg.in/yaml.v3`
96
96
97
97
The `Version` and `RelaxedVersion` have the YAML un/marshaler implemented so they can be YAML decoded/encoded with the excellent `gopkg.in/yaml.v3` library.
98
+
99
+
## Lexicographic sortable strings that keeps semantic versioning order
100
+
101
+
The `Version` and `RelaxedVersion` objects provides the `SortableString()` method that returns a string with a peculiar property: the alphanumeric sorting of two `Version.SortableString()` matches the semantic versioning ordering of the underling `Version` objects. In other words, given two `Version` object `a` and `b`:
102
+
* if `a.LessThan(b)` is true then `a.SortableString() < b.SortableString()` is true and vice-versa.
103
+
* if `a.Equals(b)` is true then `a.SortableString() == b.SortableString()` is true and vice-versa.
104
+
* more generally, the following assertion is always true: `a.CompareTo(b) == cmp.Compare(a.SortableString(), b.SortableString())`
105
+
106
+
This is accomplished by adding some adjustment characters to the original semver `Version` string with the purpose to change the behaviour of the natural alphabetic ordering, in particular:
107
+
* to allow comparison of numeric values (keeping digits aligned by unit, tenths, hundhereds, etc...).
108
+
* to invert the ordering of versions with and without prelease (a version with prelease should be lower priority compared to the same version without prerelease, but being longer alphanumerically it naturally follows it).
109
+
110
+
To give you an idea on how it works, the following table shows some examples of semver versions and their `SortableString` counter part:
111
+
112
+
| semver |`SortableString()`|
113
+
| ------------------ | ------------------ |
114
+
|`1.2.4`|`1.2.4;`|
115
+
|`1.3.0-rc`|`1.3.0-;rc`|
116
+
|`1.3.0-rc.0`|`1.3.0-;rc.:0`|
117
+
|`1.3.0-rc.5`|`1.3.0-;rc.:5`|
118
+
|`1.3.0-rc.5+build`|`1.3.0-;rc.:5`|
119
+
|`1.3.0-rc.20`|`1.3.0-;rc.::20`|
120
+
|`1.3.0`|`1.3.0;`|
121
+
|`1.20.0`|`1.:20.0;`|
122
+
|`1.90.0`|`1.:90.0;`|
123
+
|`1.300.0-6`|`1.::300.0-:6`|
124
+
|`1.300.0-30`|`1.::300.0-::30`|
125
+
|`1.300.0-1pre`|`1.::300.0-;1pre`|
126
+
|`1.300.0-pre`|`1.::300.0-;pre`|
127
+
|`1.300.0`|`1.::300.0;`|
128
+
129
+
The `SortableString()` can be used in SQL databases to simplify the ordering of a set of versions in a table.
0 commit comments