# Enum.sort(enumerable, module) - why not Enum.sort(enumerable, comparable)

**URL:** <https://forum.elixirforum.com/t/enum-sort-enumerable-module-why-not-enum-sort-enumerable-comparable/30899>\
**Category:** Discussions\
**Tags:** protocols, enum\
**Created:** [April 23, 2020, 6:00pm UTC](https://forum.elixirforum.com/t/enum-sort-enumerable-module-why-not-enum-sort-enumerable-comparable/30899 "2020-04-23T18:00:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marcus](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/marcus/32/24640_2.png) [@Marcus](https://forum.elixirforum.com/u/Marcus)\
**Post date:** [April 23, 2020, 6:00pm UTC](https://forum.elixirforum.com/t/enum-sort-enumerable-module-why-not-enum-sort-enumerable-comparable/30899/1 "2020-04-23T18:00:36Z")

</div>

Hello,

I have taken a look at `Enum.sort` and it is great to have the option to give the function a `module` to use the `module.compare/2` function. But, wouldn’t it be nice if the module implemented a `Comparable` protocol?  
In this case, `Comparable` would be a Protocol with just `compare/2` as the only function.

What do you think?

---

<div class="post-metadata">

**Author:** ![benwilson512](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/benwilson512/32/1457_2.png) [@benwilson512](https://forum.elixirforum.com/u/benwilson512)\
**Post date:** [April 23, 2020, 6:26pm UTC](https://forum.elixirforum.com/t/enum-sort-enumerable-module-why-not-enum-sort-enumerable-comparable/30899/2 "2020-04-23T18:26:47Z")

</div>

Hi @marcus there have been some proposals for a comparable protocol before on the mailing list: [Redirecting to Google Groups](https://groups.google.com/forum/#!searchin/elixir-lang-core/comparable%7Csort:date/elixir-lang-core/eE_mMWKdVYY/A5n0vuPpAQAJ)

---

<div class="post-metadata">

**Author:** ![Qqwy](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/qqwy/32/1349_2.png) [@Qqwy](https://forum.elixirforum.com/u/Qqwy)\
**Post date:** [April 23, 2020, 7:33pm UTC](https://forum.elixirforum.com/t/enum-sort-enumerable-module-why-not-enum-sort-enumerable-comparable/30899/3 "2020-04-23T19:33:52Z")

</div>

As for your specific suggestion: An immediate problem is that we might want to compare two different datatypes with one-another. Now the question becomes: which protocol implementation to call?

Passing a module (and using `compare` as a “behaviour” function) is more flexible and handles these kinds of situations without problem.

---

<div class="post-metadata">

**Author:** ![al2o3cr](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/al2o3cr/32/3457_2.png) [@al2o3cr](https://forum.elixirforum.com/u/al2o3cr)\
**Post date:** [April 24, 2020, 12:34am UTC](https://forum.elixirforum.com/t/enum-sort-enumerable-module-why-not-enum-sort-enumerable-comparable/30899/4 "2020-04-24T00:34:00Z")

</div>

> [@Marcus](#):
>
> But, wouldn’t it be nice if the module implemented a `Comparable` protocol?

There’s a certain amount of overhead for protocol dispatch. I haven’t measured how _much_, but there’s certainly more work for the compiler.

Another reason (I suspect) is historical: a lot of data types had a `compare` function even before `sort` supported this option, so most of the protocol implementations would be trivial:

```elixir
defimpl Comparable, for: DateTime do
  def compare(d1, d2), do: DateTime.compare(d1, d2)
end

```

But the subtlest reason is because protocol dispatch isn’t symmetric in its arguments if they aren’t the same type. For instance, if you have the following:

```elixir
defimpl Comparable, for: A do
  def compare(a1, a2), do: ...
end

defimpl Comparable, for: B do
  def compare(b1, b2), do: ...
end

```

Then sorting a list of mixed `A` and `B` structs will dispatch calls like `Comparable.compare(a, b)` and `Comparable.compare(b, a)` - which lead to DIFFERENT functions based on the first argument.
