How do "-" (hyphens) in the middle of pre-release and build numbers get sorted? #58

Closed
opened 2026-02-17 11:06:58 -06:00 by GiteaMirror · 4 comments
Owner

Originally created by @jasongregori on GitHub (Mar 14, 2013).

According to items 9 and 10: pre-release and build numbers can have "-" in them.
According to item 11: "Pre-release and build version precedence MUST be determined by comparing each dot separated identifier as follows: identifiers consisting of only digits are compared numerically and identifiers with letters or hyphens are compared lexically in ASCII sort order". I take this to mean that you should separate everything at each "." and compare those pieces separately according to those rules.

My question: is "1.0.0-rc.2-beta < 1.0.0-rc.11-beta" correct?

I would think this would be true, but if I separated by dots I eventually get to "2-beta" vs "11-beta". Following the instructions of "identifiers with letters or hyphens are compared lexically in ASCII sort order", "2-beta" would be > "11-beta" so the above would be incorrect.

Personally, I don't like being able to have hyphens in pre-release and build numbers even makes much sense. But I want to sort these and I want to do it correctly.

Thanks!

Originally created by @jasongregori on GitHub (Mar 14, 2013). According to items 9 and 10: pre-release and build numbers can have "-" in them. According to item 11: "Pre-release and build version precedence MUST be determined by comparing each dot separated identifier as follows: identifiers consisting of only digits are compared numerically and identifiers with letters or hyphens are compared lexically in ASCII sort order". I take this to mean that you should separate everything at each "." and compare those pieces separately according to those rules. My question: is "1.0.0-rc.2-beta < 1.0.0-rc.11-beta" correct? I would think this would be true, but if I separated by dots I eventually get to "2-beta" vs "11-beta". Following the instructions of "identifiers with letters or hyphens are compared lexically in ASCII sort order", "2-beta" would be > "11-beta" so the above would be incorrect. Personally, I don't like being able to have hyphens in pre-release and build numbers even makes much sense. But I want to sort these and I want to do it correctly. Thanks!
Author
Owner

@haacked commented on GitHub (Mar 15, 2013):

Hi @jasongregori. - hyphen is ASCII 45 which means it does precede 0-9A-Za-z. So it would seem that:

1.0.0-rc.2-beta < 1.0.0-rc.11-beta is not correct. As you point out, ASCII sort order would place 2-beta after 11-beta.

Clearly that goes against what people might expect so I'd just recommend doing this:

1.0.0-rc.2.beta < 1.0.0-rc.11.beta which sorts in the way you expect.

@haacked commented on GitHub (Mar 15, 2013): Hi @jasongregori. `-` hyphen is ASCII 45 which means it does precede `0-9A-Za-z`. So it would seem that: `1.0.0-rc.2-beta` < `1.0.0-rc.11-beta` is _not correct_. As you point out, ASCII sort order would place `2-beta` after `11-beta`. Clearly that goes against what people might expect so I'd just recommend doing this: `1.0.0-rc.2.beta` < `1.0.0-rc.11.beta` which sorts in the way you expect.
Author
Owner

@EddieGarmon commented on GitHub (Mar 15, 2013):

Then doesn't it make more sense to remove it from the allowed characters, than to leave it and have confused consumers?

@EddieGarmon commented on GitHub (Mar 15, 2013): Then doesn't it make more sense to remove it from the allowed characters, than to leave it and have confused consumers?
Author
Owner

@haacked commented on GitHub (Mar 15, 2013):

Not necessarily. You may have good reasons to use it. 1.0.0-experimental-beta Also, you have the same problem anyways without the dash: 1.0.0-beta11 < 1.0.0-beta2.

I think there will always be cases where it's possible to do something confusing and we can't prevent them all.

BTW, I'm closing this unless there's an actionable item. I believe I answered the original question. Thanks!

@haacked commented on GitHub (Mar 15, 2013): Not necessarily. You may have good reasons to use it. `1.0.0-experimental-beta` Also, you have the same problem anyways without the dash: `1.0.0-beta11` < `1.0.0-beta2`. I think there will always be cases where it's possible to do something confusing and we can't prevent them all. BTW, I'm closing this unless there's an actionable item. I believe I answered the original question. Thanks!
Author
Owner

@jasongregori commented on GitHub (Mar 15, 2013):

Thanks @Haacked, it made sense to me, I was just looking for a confirmation from someone with authority.

@jasongregori commented on GitHub (Mar 15, 2013): Thanks @Haacked, it made sense to me, I was just looking for a confirmation from someone with authority.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/semver#58