# Come recuperare argomenti ordinati per valore del campo personalizzato iso8601?

**URL:** <https://meta.discourse.org/t/how-to-fetch-topics-ordered-by-custom-field-iso8601-value/166343>\
**Category:** Development\
**Created:** [6 Ottobre 2020, 7:18am UTC](https://meta.discourse.org/t/how-to-fetch-topics-ordered-by-custom-field-iso8601-value/166343 "2020-10-06T07:18:02Z")\
**Posts on this page:** 1\
**Showing post:** 6

<div class="post-metadata">

**Author:** ![Falco](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/falco/32/179432_2.png) [@Falco](https://meta.discourse.org/u/Falco)\
**Post date:** [6 Ottobre 2020, 4:16pm UTC](https://meta.discourse.org/t/how-to-fetch-topics-ordered-by-custom-field-iso8601-value/166343/6 "2020-10-06T16:16:21Z")

</div>

> [@Stefan\_Brighiu\_M](#):
>
> ` .order("case when custom_fields.value IS NULL then topics.created_at else cast(custom_fields.value as timestamp) end desc")`

Dovresti trasformarlo in:

```plaintext
.order("COALESCE(CAST(custom_fields.value AS timestamp), custom_fields.value) DESC")

```

per una migliore leggibilità.

> [@Stefan\_Brighiu\_M](#):
>
> Non riesco proprio a capire perché diavolo la prima voce non sia alla fine

Ordini per `sortDate` e, quando manca, usi `createdAt`. Tutto in ordine decrescente. La query restituisce i dati esattamente come richiesto, poiché il 2020 è “maggiore” del secondo posto del 2011.

Se desideri che i valori null appaiano per ultimi, devi usare:

```plaintext
.order("CAST(custom_fields.value AS timestamp) DESC NULLS LAST")

```

---

_[View the full topic](https://meta.discourse.org/t/how-to-fetch-topics-ordered-by-custom-field-iso8601-value/166343)._
