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.18.3

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.

kip

kip

ex_cldr Core Team

@LostKobrakai said it very well. The only thing for me to add is that I have been back researching implementation designs for a native Elixir collation implementation. It’s not a small effort to get right (space and time efficient). The Unicode Collation Algorithm is comprehensive, and has some tuning in the CLDR Collation implementation. I’m optimistic I’ll have something to ship this year but I can’t commit to that yet. Collaboration and contribution would be very warmly welcomed.

expilo

expilo

[“Łódź”, “Warszawa”, “Zegrze”] |> Enum.sort

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

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...
3268 119930 1237
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
dotdotdotPaul
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
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
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
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
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

We're in Beta

About us Mission Statement