How to sort list of structs based on value in stucts

sort
enum

#1

Hey I have a list of structs like these:

%App.Accounts.Event{
    __meta__: #Ecto.Schema.Metadata<:loaded, "events">,
    datetime: ~D[2018-12-17],
    description: "hehexd",
    id: 10,
    inserted_at: ~N[2018-11-07 05:01:59.029747],
    private: true,
    title: "privevedsant2",
    updated_at: ~N[2018-11-07 05:01:59.029754],
    user: #Ecto.Association.NotLoaded<association :user is not loaded>,
    user_id: 1
  }

and I would like to sort this list based on datetime, how would one go about this?


#2

this does the trick:

    Enum.sort(unordered_events, fn x, y ->
            case Date.compare(x.datetime, y.datetime) do
              :lt -> true
              _ -> false
            end
          end)

#3
Enum.sort(unordered, fn %{datetime: x}, %{datetime: y} -> :lt == Date.compare(x, y) end)

Patternmatching is optional though, but Doing :lt == … makes it better readable than the case/2 in my opinion.


#4

What is the query that generated those results?

Ecto.Query.order_by/3

i.e. let the database do it - it’s good at it …

# file: music_db/priv/repo/playground.exs
#
# http://www.pragmaticprogrammer.com/titles/wmecto
# https://pragprog.com/titles/wmecto/source_code
# http://media.pragprog.com/titles/wmecto/code/wmecto-code.zip
#
# pg_ctl -D /usr/local/var/postgres start
# mix format ./priv/repo/playground.exs
# mix run ./priv/repo/playground.exs
#
defmodule Playground do
  import Ecto.Query
  alias MusicDB.Repo
  alias MusicDB.{Album, Track}

  def show(title, f) do
    title
    |> f.()
    |> IO.inspect()
  end

  def play do
    title = "Live At Montreaux"
    show(title, &list_by_join/1)
    show(title, &list_by_assoc/1)
    show(title, &list_with_preload/1)
  end

  def list_by_join(title) do
    from(a in Album,
      inner_join: t in Track,
      on: t.album_id == a.id,
      where: a.title == ^title,
      order_by: [asc: a.id, asc: t.index],
      select: t
    )
    |> Repo.all()
  end

  def list_by_assoc(title) do
    from(a in Album,
      inner_join: t in assoc(a, :tracks),
      where: a.title == ^title,
      order_by: [asc: a.id, asc: t.index],
      select: t
    )
    |> Repo.all()
  end

  def list_with_preload(title) do
    from(a in Album,
      inner_join: t in assoc(a, :tracks),
      where: a.title == ^title,
      preload: [tracks: t],
      order_by: [asc: a.id, asc: t.index]
    )
    |> Repo.all()
  end
end

Playground.play()
$ mix run ./priv/repo/playground.exs

09:59:15.617 [debug] QUERY OK source="albums" db=3.0ms

SELECT
  t1."id",
  t1."title",
  t1."duration",
  t1."index",
  t1."number_of_plays",
  t1."inserted_at",
  t1."updated_at",
  t1."album_id"
FROM "albums" AS a0
  INNER JOIN "tracks" AS t1 ON t1."album_id" = a0."id"
WHERE (a0."title" = $1)
  ORDER BY a0."id", t1."index"
["Live At Montreaux"]

[
  %MusicDB.Track{
    __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
    album: #Ecto.Association.NotLoaded<association :album is not loaded>,
    album_id: 5,
    duration: 761,
    duration_string: nil,
    id: 30,
    index: 1,
    inserted_at: ~N[2018-11-06 13:40:37.121579],
    number_of_plays: 0,
    title: "Anton's Ball",
    updated_at: ~N[2018-11-06 13:40:37.121586]
  },
  %MusicDB.Track{
    __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
    album: #Ecto.Association.NotLoaded<association :album is not loaded>,
    album_id: 5,
    duration: 647,
    duration_string: nil,
    id: 31,
    index: 2,
    inserted_at: ~N[2018-11-06 13:40:37.122448],
    number_of_plays: 0,
    title: "The Moontrane",
    updated_at: ~N[2018-11-06 13:40:37.122455]
  },
  %MusicDB.Track{
    __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
    album: #Ecto.Association.NotLoaded<association :album is not loaded>,
    album_id: 5,
    duration: 805,
    duration_string: nil,
    id: 32,
    index: 3,
    inserted_at: ~N[2018-11-06 13:40:37.123026],
    number_of_plays: 0,
    title: "Farallone",
    updated_at: ~N[2018-11-06 13:40:37.123032]
  },
  %MusicDB.Track{
    __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
    album: #Ecto.Association.NotLoaded<association :album is not loaded>,
    album_id: 5,
    duration: 844,
    duration_string: nil,
    id: 33,
    index: 4,
    inserted_at: ~N[2018-11-06 13:40:37.123612],
    number_of_plays: 0,
    title: "Song Of Songs",
    updated_at: ~N[2018-11-06 13:40:37.123617]
  }
]

09:59:15.630 [debug] QUERY OK source="albums" db=2.5ms

SELECT
  t1."id",
  t1."title",
  t1."duration",
  t1."index",
  t1."number_of_plays",
  t1."inserted_at",
  t1."updated_at",
  t1."album_id"
FROM "albums" AS a0
  INNER JOIN "tracks" AS t1 ON t1."album_id" = a0."id"
WHERE (a0."title" = $1)
ORDER BY a0."id", t1."index"
["Live At Montreaux"]

[
  %MusicDB.Track{
    __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
    album: #Ecto.Association.NotLoaded<association :album is not loaded>,
    album_id: 5,
    duration: 761,
    duration_string: nil,
    id: 30,
    index: 1,
    inserted_at: ~N[2018-11-06 13:40:37.121579],
    number_of_plays: 0,
    title: "Anton's Ball",
    updated_at: ~N[2018-11-06 13:40:37.121586]
  },
  %MusicDB.Track{
    __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
    album: #Ecto.Association.NotLoaded<association :album is not loaded>,
    album_id: 5,
    duration: 647,
    duration_string: nil,
    id: 31,
    index: 2,
    inserted_at: ~N[2018-11-06 13:40:37.122448],
    number_of_plays: 0,
    title: "The Moontrane",
    updated_at: ~N[2018-11-06 13:40:37.122455]
  },
  %MusicDB.Track{
    __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
    album: #Ecto.Association.NotLoaded<association :album is not loaded>,
    album_id: 5,
    duration: 805,
    duration_string: nil,
    id: 32,
    index: 3,
    inserted_at: ~N[2018-11-06 13:40:37.123026],
    number_of_plays: 0,
    title: "Farallone",
    updated_at: ~N[2018-11-06 13:40:37.123032]
  },
  %MusicDB.Track{
    __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
    album: #Ecto.Association.NotLoaded<association :album is not loaded>,
    album_id: 5,
    duration: 844,
    duration_string: nil,
    id: 33,
    index: 4,
    inserted_at: ~N[2018-11-06 13:40:37.123612],
    number_of_plays: 0,
    title: "Song Of Songs",
    updated_at: ~N[2018-11-06 13:40:37.123617]
  }
]

09:59:15.632 [debug] QUERY OK source="albums" db=1.6ms

SELECT
  a0."id",
  a0."title",
  a0."inserted_at",
  a0."updated_at",
  a0."artist_id",
  t1."id",
  t1."title",
  t1."duration",
  t1."index",
  t1."number_of_plays",
  t1."inserted_at",
  t1."updated_at",
  t1."album_id"
FROM "albums" AS a0
  INNER JOIN "tracks" AS t1 ON t1."album_id" = a0."id"
WHERE
  (a0."title" = $1)
  ORDER BY a0."id", t1."index"
["Live At Montreaux"]

[
  %MusicDB.Album{
    __meta__: #Ecto.Schema.Metadata<:loaded, "albums">,
    artist: #Ecto.Association.NotLoaded<association :artist is not loaded>,
    artist_id: 3,
    genres: #Ecto.Association.NotLoaded<association :genres is not loaded>,
    id: 5,
    inserted_at: ~N[2018-11-06 13:40:37.118081],
    title: "Live At Montreaux",
    tracks: [
      %MusicDB.Track{
        __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
        album: #Ecto.Association.NotLoaded<association :album is not loaded>,
        album_id: 5,
        duration: 761,
        duration_string: nil,
        id: 30,
        index: 1,
        inserted_at: ~N[2018-11-06 13:40:37.121579],
        number_of_plays: 0,
        title: "Anton's Ball",
        updated_at: ~N[2018-11-06 13:40:37.121586]
      },
      %MusicDB.Track{
        __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
        album: #Ecto.Association.NotLoaded<association :album is not loaded>,
        album_id: 5,
        duration: 647,
        duration_string: nil,
        id: 31,
        index: 2,
        inserted_at: ~N[2018-11-06 13:40:37.122448],
        number_of_plays: 0,
        title: "The Moontrane",
        updated_at: ~N[2018-11-06 13:40:37.122455]
      },
      %MusicDB.Track{
        __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
        album: #Ecto.Association.NotLoaded<association :album is not loaded>,
        album_id: 5,
        duration: 805,
        duration_string: nil,
        id: 32,
        index: 3,
        inserted_at: ~N[2018-11-06 13:40:37.123026],
        number_of_plays: 0,
        title: "Farallone",
        updated_at: ~N[2018-11-06 13:40:37.123032]
      },
      %MusicDB.Track{
        __meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
        album: #Ecto.Association.NotLoaded<association :album is not loaded>,
        album_id: 5,
        duration: 844,
        duration_string: nil,
        id: 33,
        index: 4,
        inserted_at: ~N[2018-11-06 13:40:37.123612],
        number_of_plays: 0,
        title: "Song Of Songs",
        updated_at: ~N[2018-11-06 13:40:37.123617]
      }
    ],
    updated_at: ~N[2018-11-06 13:40:37.118088]
  }
]
$

#5

Yes, I prefer that too, but I combined the list form 3 separate lists so it has to be done this way, but thank you for the answer :smile: !


#6

As long as those are separate by design rather than accident :wink:

Ecto 3 introduced union/2 and union_all/2.


#7

they are :smile: but this union seems very interesting, i don’t think I can apply to my case here, since I need the 3 separately to have them ordered in another way in another case, also I am querying the same table just based on different select arguments like in one something needs to be false but in other true etc, and check for id etc or not have the id, so for now for me it is the easiest way to read it and have it working maybe i could use the union for 2 but not for all