benonymus

benonymus

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?

Showing Posts 1 to 6

benonymus

benonymus OP

this does the trick:

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

NobbZ

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.

peerreynders

peerreynders

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]
  }
]
$
benonymus

benonymus OP

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: !

peerreynders

peerreynders

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

Ecto 3 introduced union/2 and union_all/2.

benonymus

benonymus OP

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

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews