vermaxik

vermaxik

List Comparison (how it works)

Hello Elixirforum,

Could you please explain how List Comparison works in Elixir?

Why such list of integers returns true

iex(15)> [3,10] > [3,3,3,3]
true

And the same example list of strings:

iex(16)> ["3","10"] > ["3", "3", "3", "3"]
false

I would expect in both examples false because of list size.

Thank you!

Marked As Solved

NobbZ

NobbZ

List comparison works by comparing element by element.

For each pair of equal elements, the comparison advances to the next. The first that is not equal decides. And 10 is greater than 3 while "10" is not greater than "3".

You can learn more in the docs:

Also Liked

LostKobrakai

LostKobrakai

Lists are compared element by element, disregarding the size (tuples are compared by size then elements). So it should come down to integers and binaries comparing differently.

iex(1)> "10" > "3"
false
iex(2)> 10 > 3
true
hauleth

hauleth

As for reasoning for this behaviour - computing size of list is as expensive as traversing whole list, so comparing two lists first by length and then by elements would make whole operation more expensive.

Last Post!

arbaz_asif

arbaz_asif

The most efficient way to compare two lists on the basis of their sizes is by using Enum.count() function from Enum Module. It will work like this:

Enum.count([3,10]) > Enum.count([3,3,3,3])
false

Enum.count(["3","10"]) > Enum.count(["3", "3", "3", "3"])
false

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 55125 245
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement