Contentful logo

Contentful Community

Ordering by Content Type Field

How do you order by content type field? There are examples of using sys fields such as createdAt but no examples of sorting using content type fields.

This is a code example in the documentation:

let query = Query.where(contentTypeId: "<content_type_id>")
try! query.order(by: Ordering(sys: .createdAt))

I am trying to achieve something like this:

let query = QueryOn<NewsDataModel>()
try! query.order(by: Ordering(NewsDataModel.Fields.date.stringValue))

I managed to achieve the result I wanted with the following code:

let query = QueryOn<NewsDataModel>()
try! query.order(by: Ordering("fields.date", inReverse: true))
1 Like

Hey @somascanapp,

Thank you so much for your reply! Using the format fields.Yourfield should indeed do the trick, as described in our documentation:

https://www.contentful.com/developers/docs/references/content-delivery-api/

Let us know if you have any other questions :slight_smile: