benonymus

benonymus

How to sort list of structs based on value in stucts

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?

Marked As Solved

benonymus

benonymus

this does the trick:

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

Also Liked

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

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

benonymus

benonymus

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

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
chewm
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 30048 115
New
peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New

We're in Beta

About us Mission Statement