Sorting By Functions in Elixir
Sometimes we’ll want to sort a collection by something other than its natural order. For example, suppose we wanted to sort strings by their length instead of alphabetically. Here’s an example of custom sorts in Elixir.
In this Elixir version, we use Enum.sort/2
which takes a collection and a sorting function. The sorting function should return true
if the first argument is considered less than or equal to the second.
For the fruits
list, we create a len_cmp
function that compares string lengths. We then use this function with Enum.sort/2
to sort the fruits by length.
For the people
list, we define a list of maps instead of structs (as Elixir doesn’t have a direct equivalent to Go’s structs). We then use Enum.sort/2
with an anonymous function that compares the age
field of each map.
Note that in Elixir, we don’t need to worry about whether to use pointers for large structs, as Elixir handles memory management automatically.
When you run this program, you should see output similar to:
This demonstrates how to implement custom sorting in Elixir, both for built-in types and custom data structures.