eksperimental

eksperimental

Hi,
we are trying to sort things naturally in ExDoc.

Can anybody recommend a natural sorting algorithm implementation in Elixir?
I have a a few but I am looking for options,
Thank you.

First 10 of 11 Posts Switch mode

Sebb

Sebb

How do you define “natural”?

Things that come to mind:

  • ignore case
  • ignore diacritics
  • 2 < 10
eksperimental

eksperimental OP

Mostly. Case should not be ignored, but order should be: A, a, B, b, and not A, B, a, b as sorting by ASCII code does.

sabiwara

sabiwara

Elixir Core Team

I have this hacky solution which should work but probably isn’t optimal:

Enum.sort_by(~w(A B a b), &{String.downcase(&1), &1})
mudasobwa

mudasobwa

Creator of Cure
eksperimental

eksperimental OP

EDIT: the implementation is correct, I had the wrong input

The problem with this approach is that it returns different results depending on the initial order for the same list shuffled.

iex(16)> Enum.sort_by(~w(A B a b), &{String.downcase(&1), &1})
["A", "a", "B", "b"]

iex(17)> Enum.sort_by(~w(a B B b), &{String.downcase(&1), &1})
["a", "B", "B", "b"]

~~Actually I have reported this very same issue to this library just a few hours ago: Issues · BinaryNoggin/natural_order · GitHub

eksperimental

eksperimental OP

Thank you Aleksei for your contribution to the discussion, later one with more time, I will look in detail and see how I can apply those to the NaturalOrder library. There are a few lessons to take from the discussion.

hauleth

hauleth

Well, this is obvious implication of the fact that Enum.sort* functions are using stable sort algorithms.

sabiwara

sabiwara

Elixir Core Team

I’m sorry if I misunderstood your comment, but this example is not using the same list shuffled, these are different lists. I believe the result provided is the expected one?

If I reuse the example from your issue it seems fine too:

iex(1)> ~W(a A b) |> Enum.sort_by(&{String.downcase(&1), &1})             
["A", "a", "b"]
iex(2)> ~W(A b a) |> Enum.sort_by(&{String.downcase(&1), &1})
["A", "a", "b"]
eksperimental

eksperimental OP

You are right. I messed up with the input. Your implementation.

adkron

adkron

We updated NaturalSort with changes from @eksperimental. Thanks for fixing those bugs.

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement