gabrielgiordano

gabrielgiordano

Version comparison with built-in comparison keywords "3.3.15.1" > "3.3.15.0"

Hello community!

I’ve been comparing some version string binaries with the built-in comparison keywords, and the results seems about right.

I assume that the kind of data type Elixir is comparing in those cases are bitstrings, right?

Do you think this is a reliable way to compare that kind of version pattern, or am I missing something?

iex(1)> "3.0.0.0.0" > "3.3.15.0"
false
iex(2)> "3.0.0.0.0" > "3.3.15.0"
false
iex(3)> "3.3.15.0" > "3.3.15.0"  
false
iex(4)> "3.3.15.1" > "3.3.15.0"
true
iex(5)> "3.3.15.1" > "3.3.15.0"
true
iex(6)> "3.3.14.9" > "3.3.15.0"
false
iex(7)> "3.3.0.0.0.0" > "3.3.15.0"
false
iex(8)> "3.3.0.0.0.0" > "3.4"     
false

By the way, I’ve checked the Version module, but the version pattern doesn’t entirely comply with SemVer, so if we use it, we’ll need to handle our version before :frowning:

Marked As Solved

LostKobrakai

LostKobrakai

If this is only numbers (no prereleases) then you use lists of numbers to compare. Their sort order should match what I imagine you want the versions to sort by:

[3, 14] > [3, 13, 9]
true

https://hexdocs.pm/elixir/operators.html#term-ordering

Also Liked

APB9785

APB9785

Creator of ECSx
def string_to_version(s) do
  s
  |> String.split(".")
  |> Enum.map(&String.to_integer/1)
end

This would give you integer lists like in LostKobraKai’s post

APB9785

APB9785

Creator of ECSx

For versions, I would expect version 2 to be lower than version 15 :slight_smile:

hauleth

hauleth

How does Version do not comply with SemVer? Especially that your versions mentioned there aren’t really SemVer compatible, as have more segments than 3 (major, minor, and patch).

If you want to compare them:

def parse_ver(str) do
  str
  |> String.split(".")
  |> Enum.map(&String.to_integer/1)
end

def compare([a | as], [b | bs]) when a == b, do: compare(as, bs)
def compare([], []), do: {:ok, :equal}
def compare([a | _], [b | _]) when a > b, do: {:ok, :greater}
def compare([a | _], [b | _]) when a < b, do: {:ok, :lesser}
def compare(_, _), do: {:error, :incomparable}

Where Next?

Popular in Questions Top

shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

yurko
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New

We're in Beta

About us Mission Statement