expilo

expilo

Sorting using locales

I can’t find any information in the docs how to use specific locale’s collation rules in sort commands available in Elixir. Does that mean I’m on my own here? If anyone tackled this problem before what did you try? Many thanks for any ideas.

Most Liked

LostKobrakai

LostKobrakai

There is no right or wrong here given a lack of context. Collations are locale specific defaults sort orders so given a lack of knowing the locale there’s no way to select a locale specific default sort order. One and the same list can be sorted differently depending on which local you sort for.

WITH test_data AS (
	SELECT val FROM (VALUES ('a'),('b'), ('c'), ('ch'), ('d'), ('e'), ('f'), ('g'), ('h'), ('i'), ('j')) AS t (val)
)
SELECT val FROM test_data ORDER BY val COLLATE "de_DE";
# a, b, c, ch, d, e, f, g, h, i, j
WITH test_data AS (
	SELECT val FROM (VALUES ('a'),('b'), ('c'), ('ch'), ('d'), ('e'), ('f'), ('g'), ('h'), ('i'), ('j')) AS t (val)
)
SELECT val FROM test_data ORDER BY val COLLATE "sk_SK";
# a, b, c, d, e, f, g, h, ch, i, j

As for the reasons why this is not part of elixir – same reason why elixir doesn’t ship a (full) timezone database – it doesn’t want to be dependant on the release cycles of the source for information needed, as well as needing to choose a source in the first place. Lots of elixirs TZ packages use IANA as their source, but IANA has different update cycles than elixir. They usually update a handful of times or more per year. Elixir does only twice. Instead Elixir exposes a config for users to provide a time zone database via the Calendar.TimeZoneDatabase behaviour. This also allows for packages to e.g. integrate with the tz handling of the underlying host OS, which is iirc what nerves does to not duplicate sources.

For sorting such an interface exists as well Enum.sort can take a module exposing a :compare/2 function as argument, which can define sorting order. There’s no behaviour, given the interface is so simple, but it’s documented here: Enum — Elixir v1.20.2

So it’s up to the community to build libraries, which allow sorting in a locale specific manner. ex_cldr_collattion is a first step in integrating with icu4c, but it’s limited atm:

This initial version uses only the “root” locale collator which is the CLDR DUCET collator.

Why hasn’t more happened yet? Probably because (as I did above) sorting often happens at the db level less so than at the application level and therefore the need might not be that large. I’m sure @kip would appreciate some help integrating some more locales though.

greven

greven

Hey. The way I do it is using the CLDR Collation lib (all CLDR libs are pretty good! Thanks @kip!)

NobbZ

NobbZ

There is nothing like that in elixir, but you can use Enum.sort/2 and define the sorting function as necessary by your collation.

Where Next?

Popular in Questions Top

sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
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
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
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
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

Other popular topics 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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126479 1222
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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