Semantic Version Comparator
Most hand-rolled version comparators split a version on dots and compare the numbers, which gets two real cases wrong. First, a pre-release build must sort BEFORE its release - "2.0.0-rc.1" is LOWER precedence than "2.0.0", the opposite of plain string or numeric ordering. Second, inside a pre-release tag, an identifier made entirely of digits compares numerically and always sorts before any alphanumeric identifier at the same position - so "rc.2" is less than "rc.10" (numeric, correct order) but "rc.2" is also less than "rc.a" (numeric-before-alphanumeric, not "2" < "a" alphabetically). This library follows semver.org section 11 precisely: core fields compare numerically, a version with no pre-release outranks the same core version with one, pre-release identifiers compare left to right (numeric identifiers numerically, alphanumeric identifiers by ASCII string order, numeric always below alphanumeric), and if every shared identifier is equal the version with FEWER pre-release identifiers has lower precedence. Build metadata ("+build.1") is parsed but explicitly excluded from precedence, per the spec. Parsing is strict, not permissive - a leading zero on a numeric field ("1.02.0"), a missing field ("1.0"), or an illegal identifier character makes Parse() return false rather than guessing. An update-checker or changelog-sorter built on a permissive parser can silently misorder versions it was never actually shown correctly. Notes and limitations - Strict parsing only - no support for common non-SemVer conventions like a leading "v" ("v1.2.3") or a 2-field "1.2" short form. Strip a leading "v" yourself before calling Parse() if your source uses it. - Build metadata is validated for legal characters but never affects ComparePrecedence() - two versions differing only in build metadata compare as equal, exactly as the spec requires. - ComparePrecedence() assumes both instances parsed successfully; calling it on an instance that failed Parse() compares whatever default/partial state it holds, which is meaningless. Always check Parse()'s return value (or IsValid()) first.
Script · MT5 · v1.0 · Free
About Semantic Version Comparator
Semantic Version Comparator is a script for MetaTrader 5. MQL source code included.
Files in the download: SemanticVersionComparator.mq5.mqh, SemanticVersionComparator.txt, SemanticVersionComparatorDemo.ex5, SemanticVersionComparatorDemo.mq5.
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 5 open File → Open Data Folder, then go to MQL5 → Scripts.
- Copy the .mq5 file(s) there. Put any .mqh files in MQL5 → Include.
- Open the .mq5 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: MT5 Script · MQL5 Source Code