Sorting an Array

I have an array and i want to sort it. I have tried Enum.sort and Enum.sort_by but havent got any valuable answer.

  [
    {1, 1, [dept: :HR, dep_id: :d1], [name: :owais, address: :tromso]},
    {1, 1, [name: :owais, address: :tromso], [dept: :HR, dep_id: :d1]}
  ]

I want it to be like

[
    {1, 1, [dept: :HR, dep_id: :d1], [name: :owais, address: :tromso]},
    {1, 1, [dept: :HR, dep_id: :d1], [name: :owais, address: :tromso]}
  ]

You don’t have arrays, but lists in Elixir. They are quite different structures and do not behave the same.

You might be used to access any position in an array with an index, it is not the case for a list, or linked list. The usual way to use a list is [head | tail] = list

And what You really want to sort in your example is a tuple… which is used for fixed position, and does not implement Enumerable protocol.

Maybe You should modify how You get those data, it might be easier to reshape before You try to sort.

3 Likes