fchabouis

fchabouis

String compare depends on upper/lower case?

I am wondering why I have this behavior, that seems counter intuitive to me:

iex(1)> "x" > "a"
true
iex(2)> "X" > "a"
false

When I sort a list of strings, I don’t expect this result

iex(1)> Enum.sort(["b", "a", "X"])
["X", "a", "b"]

I didn’t find in the documentation where this behavior is explained. If somebody knows, I’m interested :slight_smile:
Thanks!

Marked As Solved

LostKobrakai

LostKobrakai

Bitstrings are compared byte by byte, incomplete bytes are compared bit by bit.

Uppercase letters use smaller byte values than lowercase ones.

Also Liked

kip

kip

ex_cldr Core Team

And also be aware that if you are sorting non-ASCII strings then you should also normalise the string first. For example String.downcase(string) |> String.normalize(:nfkd).

Lastly, collation rules are language and culture dependent even for the same strings so depending on what you’re trying to do this is a much more complex topic than it seems on the surface.

sbuttgereit

sbuttgereit

Just off the cuff… maybe something like this.

iex(4)> Enum.sort(["b", "X", "a"], &(String.downcase(&1) <= String.downcase(&2)))
["a", "b", "X"]

There could be some string handling caveats that I’m not thinking about, but it’s the general idea.

derpycoder

derpycoder

JavaScript has the same behavior as well:

["a", "b", "X"].sort();          // ["X", "a", "b"]

Internally the Ascii values are being compared, which you can check in iex:

iex(15)> 'X'
[88]
iex(16)> 'a'
[97]
iex(17)> 'x'
[120]

So, always convert to lowercase before comparing, to avoid running into edge cases.

For instance, see UpperCase win:

iex(22)> ["derpycoder", "Derpycoder", "DerpyCoder"] |> Enum.sort()
["DerpyCoder", "Derpycoder", "derpycoder"]

See the answer by @sbuttgereit.

Where Next?

Popular in Questions Top

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
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
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
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

Other popular topics Top

lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement