ProductPromotion
Logo

Elixir

made by https://0x3d.site

GitHub - DanCouper/natural_sort: Elixir natural sort implementation for lists of strings.
Elixir natural sort implementation for lists of strings. - DanCouper/natural_sort
Visit Site

GitHub - DanCouper/natural_sort: Elixir natural sort implementation for lists of strings.

GitHub - DanCouper/natural_sort: Elixir natural sort implementation for lists of strings.

NaturalSort

NOTE v0.2 used an explicit second argument (case_sensitive). This has been removed and replaced with an options keyword list, this is a breaking change.

Sort a list of strings containing numbers in a natural manner.

Sort functions will not [generally] sort strings containing numbers the same way a human would.

Given a list:

["a10",  "a05c",  "a1",  "a",  "a2",  "a1a",  "a0",  "a1b",  "a20"]

Applying standard sort will produce:

["a", "a0", "a05c", "a1", "a10", "a1a", "a1b", "a2", "a20"]

But applying a natural sort will give:

["a", "a0", "a1", "a1a", "a1b", "a2", "a05c", "a10", "a20"]

Functions

Just the one:

NaturalSort.sort(list, options \\ [])

Sorts a list of strings (ascending). This works by leveraging Elixir's Enum.sort_by/3 function (which takes as the second argument a mapping function). The mapping operation converts each string into a list of strings and integers. Once in this form, applying the sort function results in a correctly sorted list.

Options

There are currently two available options (passed as a keyword list), :direction and case_sensitive.

  • :direction may have a value of :asc or :desc, and defaults to :asc.
  • :case_sensitive may be true or false, and defaults to false.

Examples

  iex> NaturalSort.sort(["x2-y7", "x8-y8", "x2-y08", "x2-g8" ])
  ["x2-g8", "x2-y7", "x2-y08", "x8-y8" ]

  iex> NaturalSort.sort(["a5", "a400", "a1"], direction: :desc)
  ["a400", "a5", "a1"]

  iex> NaturalSort.sort(["foo03.z", "foo45.D", "foo06.a", "foo06.A", "foo"], case_sensitive: :true)
  ["foo", "foo03.z", "foo06.A", "foo06.a", "foo45.D"]

  iex> NaturalSort.sort(["foo03.z", "foo45.D", "foo06.a", "foo06.A", "foo"], [case_sensitive: :true, direction: :desc])
  ["foo45.D", "foo06.a", "foo06.A", "foo03.z", "foo"]

Prior art:

VersionEye's naturalsorter gem was the inspiration, with that being based on Martin Pool's natural sorting algorithm, and making direct use of the Ruby implementation of the original C version.

Elixir's Version module does a similar thing.

Todo/Review

  • REVIEW: Benchmark further.
  • ENHANCEMENT: Add options: choice to use unicode, choice to strip whitespace from result.
  • ENHANCEMENT: Stream rather than map - this was designed to aid me in organising vast amounts of files by name; mapping over large lists seems inefficient.

Articles
to learn more about the elixir concepts.

Resources
which are currently available to browse on.

mail [email protected] to add your project or resources here 🔥.

FAQ's
to know more about the topic.

mail [email protected] to add your project or resources here 🔥.

Queries
or most google FAQ's about Elixir.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory