<p>Thanks to brad I got the whole thing working. 
Here's the stripped down code: </p>

<pre><code>{#

    Get all Dates from within the table field in the section veranstaltungen
    ------------------------------------------------------------------------
    We have a section called veranstaltungen with a table field that contains dates and times
    In order to get all occurences of the dates wihin the table field, we first get all of the
    table dates and attach the entry to each table date.
    Then we sort this array (dates) first by day and within a day by time.

    This is a simple Calendar without the need for a plugin.

#}
{% set entries = craft.entries({
    section: 'veranstaltungen',
    with: [
        'cf_veranstaltungs_cat',
        'cf_veranstaltungsDatum_table',
        'cf_veranstaltung_asset',
        'cf_veranstaltungsLinks_table',
        'cf_veranstaltungs_assets',
        'cf_coVeranstalter_user'
    ],

    relatedTo : relation_param,
    authorId: veranstalter_param|trim,

    limit: null
}) %}

{#
    The dates Array will contain all occurences of the entries - we will order it first by date and then by time
#}
{% set dates = [] %}

{% for entry in entries %}

    {% set table = entry.cf_veranstaltungsDatum_table %}

    {% for row in table if row.startDate %}
        {% set dates = dates|merge(
            [{
                'date': row.startDate|date('Y-m-d'),
                'time': row.startTime|date('G'),
                'startDate': row.startDate,
                'startTime': row.startTime|date('G:i'),
                'endTime': row.endTime|date('G:i'),
                'entry': entry,
                'links': entry.cf_veranstaltungsLinks_table,
                'image': entry.cf_veranstaltung_asset,
                'files': entry.cf_veranstaltungs_assets,
                'categories': entry.cf_veranstaltungs_cat ?? null,
                'categoryColor': entry.cf_veranstaltungs_cat[0].cf_veranstaltungsCatColor ?? null,
                'categoryIcon': entry.cf_veranstaltungs_cat[0].cf_veranstaltungsIcon_asset.first.url ?? null,
                'author': entry.author,
                'coVeranstalter': entry.cf_coVeranstalter_user,
                'id': entry.id,
            }]
        ) %}
    {% endfor %}
{% endfor %}


{#
    ------------------------
    Veranstaltungen Listing
    ------------------------
#}

&lt;div id="list_container"&gt;

    {% set datesSortedByDay = dates|group('date')|supersort('ksort', 'date') %}
    {% for key, day in datesSortedByDay %}

        &lt;h2&gt;{{ key|date('l, d. F') }}&lt;/h2&gt;

        {% for date in day|supersort('sort', 'time') %}
            {% include "veranstaltungen/_common_tableDates" %}
        {% endfor %}
    {% endfor %}
&lt;/div&gt;
</code></pre>

<p>Crazy what you can do with twig! </p>

CC BY-SA 4.0
2018-05-22T19:55:12.993Z
I guess I still have a N+1 Problem, because the page loads with 240 queries, and the dates array has a size of 153. I've tried to eager load everything possible, but must have forgotten something. maybe somebody can spot the error?
outline4
CC BY-SA 4.0
2018-05-22T19:52:27.013Z
2018-05-22T19:52:27.013Z
outline4
<p>I have a section called "events" in which I have a table field that contains dates and times of these events. </p>

<p>So I want to order my events by date and within each day by time. </p>

<p>I thought of mergin an array that contains only the basic stuff and the Id to output the rest of the entry. (yes! I will cache the template). </p>

<p>This is how far I got, but I am struggling on how to first order by date, and then within a day, by time. </p>

<pre><code>{% set entries = craft.entries.section('events').limit(null) %}

{% set dates = [] %}

{% for entry in entries %}
    {% set table = entry.cf_dates_table %}

    {% for row in table if row.startDate %}
        {% set dates = dates|merge(
            [{
                'startDate': row.startDate,
                'startTime': row.startTime|date('G:i'),
                'endTime': row.endTime|date('G:i'),
                'id': entry.id,
                'uniqueId': entry.id ~ '-' ~ loop.index,
            }]
        )%}
    {% endfor %}
{% endfor %}


&lt;div id="list_container"&gt;
    {% for date in dates|sort() %}
        {{ date.startDate|date('l, d. F') }} {{ date.startTime }} – {{ date.endTime }}, {{ date.id }}&lt;br /&gt;
    {% endfor %}
&lt;/div&gt;
</code></pre>

<p>I could imagine grouping the array by the day and then sort them within each day. But I can't get it to work. </p>

<p>Could somebody point me into the right direction?<br>
Is this a viable approach? </p>

<p>Cheers<br>
Stefan</p>

CC BY-SA 4.0
2018-05-18T21:45:35.447Z
This seems pretty painful to pull off in Twig alone given that Twig's [sort](https://twig.symfony.com/doc/2.x/filters/sort.html) method only supports PHP's `asort()`.  Maybe you could pull it off with something like https://github.com/TopShelfCraft/SuperSort#advanced-as-sorting
Brad Bell
CC BY-SA 4.0
2018-05-18T21:46:24.987Z
Thought about trying it from a PHP from a custom plugin?
Brad Bell
<array><sorting><group><sort>
Order twig array by date and time
# Order twig array by date and time

(Asked by: username_0 on 2018-05-18)

I have a section called "events" in which I have a table field that contains dates and times of these events. 
So I want to order my events by date and within each day by time. 
I thought of mergin an array that contains only the basic stuff and the Id to output the rest of the entry. (yes! I will cache the template). 
This is how far I got, but I am struggling on how to first order by date, and then within a day, by time. 
{% set entries = craft.entries.section('events').limit(null) %}

{% set dates = [] %}

{% for entry in entries %}
    {% set table = entry.cf_dates_table %}

    {% for row in table if row.startDate %}
        {% set dates = dates|merge(
            [{
                'startDate': row.startDate,
                'startTime': row.startTime|date('G:i'),
                'endTime': row.endTime|date('G:i'),
                'id': entry.id,
                'uniqueId': entry.id ~ '-' ~ loop.index,
            }]
        )%}
    {% endfor %}
{% endfor %}



    {% for date in dates|sort() %}
        {{ date.startDate|date('l, d. F') }} {{ date.startTime }} – {{ date.endTime }}, {{ date.id }}
    {% endfor %}


I could imagine grouping the array by the day and then sort them within each day. But I can't get it to work. 
Could somebody point me into the right direction?
Is this a viable approach? 
Cheers
Stefan

**Tags:** array, sorting, group, sort

> **Comments:**
> **username_1**: This seems pretty painful to pull off in Twig alone given that Twig's [sort](https://twig.symfony.com/doc/2.x/filters/sort.html) method only supports PHP's `asort()`.  Maybe you could pull it off with something like https://github.com/TopShelfCraft/SuperSort#advanced-as-sorting  
> **username_1**: Thought about trying it from a PHP from a custom plugin?  

---

## 1 answers

### Answer by username_0 on 2018-05-22. Score: 0 | Accepted answer

Thanks to brad I got the whole thing working. 
Here's the stripped down code: 
{#

    Get all Dates from within the table field in the section veranstaltungen
    ------------------------------------------------------------------------
    We have a section called veranstaltungen with a table field that contains dates and times
    In order to get all occurences of the dates wihin the table field, we first get all of the
    table dates and attach the entry to each table date.
    Then we sort this array (dates) first by day and within a day by time.

    This is a simple Calendar without the need for a plugin.

#}
{% set entries = craft.entries({
    section: 'veranstaltungen',
    with: [
        'cf_veranstaltungs_cat',
        'cf_veranstaltungsDatum_table',
        'cf_veranstaltung_asset',
        'cf_veranstaltungsLinks_table',
        'cf_veranstaltungs_assets',
        'cf_coVeranstalter_user'
    ],

    relatedTo : relation_param,
    authorId: veranstalter_param|trim,

    limit: null
}) %}

{#
    The dates Array will contain all occurences of the entries - we will order it first by date and then by time
#}
{% set dates = [] %}

{% for entry in entries %}

    {% set table = entry.cf_veranstaltungsDatum_table %}

    {% for row in table if row.startDate %}
        {% set dates = dates|merge(
            [{
                'date': row.startDate|date('Y-m-d'),
                'time': row.startTime|date('G'),
                'startDate': row.startDate,
                'startTime': row.startTime|date('G:i'),
                'endTime': row.endTime|date('G:i'),
                'entry': entry,
                'links': entry.cf_veranstaltungsLinks_table,
                'image': entry.cf_veranstaltung_asset,
                'files': entry.cf_veranstaltungs_assets,
                'categories': entry.cf_veranstaltungs_cat ?? null,
                'categoryColor': entry.cf_veranstaltungs_cat[0].cf_veranstaltungsCatColor ?? null,
                'categoryIcon': entry.cf_veranstaltungs_cat[0].cf_veranstaltungsIcon_asset.first.url ?? null,
                'author': entry.author,
                'coVeranstalter': entry.cf_coVeranstalter_user,
                'id': entry.id,
            }]
        ) %}
    {% endfor %}
{% endfor %}


{#
    ------------------------
    Veranstaltungen Listing
    ------------------------
#}



    {% set datesSortedByDay = dates|group('date')|supersort('ksort', 'date') %}
    {% for key, day in datesSortedByDay %}

        {{ key|date('l, d. F') }}

        {% for date in day|supersort('sort', 'time') %}
            {% include "veranstaltungen/_common_tableDates" %}
        {% endfor %}
    {% endfor %}


Crazy what you can do with twig!

> **Comments:**
> **username_0**: I guess I still have a N+1 Problem, because the page loads with 240 queries, and the dates array has a size of 153. I've tried to eager load everything possible, but must have forgotten something. maybe somebody can spot the error?  

---

