Custom query from code (sorting issue)

Hi!

I am building my blog according to your tutorial, however, in the content api I have the options to sort it by date.

https://cdn.builder.io/api/v2/content/blog-article?apiKey=32f148657e2646be8562eb4e6ebfa190&limit=10&sort.data.date=-1

I want to achieve the same in my next.js code, but the sort options is not available?

These are the options:

export interface GetContentOptions {
    userAttributes?: UserAttributes;
    /**
     * Alias for userAttributes.urlPath except it can handle a full URL (optionally with host,
     * protocol, query, etc) and we will parse out the path.
     */
    url?: string;
    /**
     * @package
     */
    includeUrl?: boolean;
    /**
     * Follow references. If you use the `reference` field to pull in other content without this
     * enabled we will not fetch that content for the final response.
     */
    includeRefs?: boolean;
    /**
     * How long in seconds content should be cached for. Sets the max-age of the cache-control header
     * response header.
     *
     * Use a higher value for better performance, lower for content that will change more frequently
     *
     * @see {@link https://www.builder.io/c/docs/query-api#__next:~:text=%26includeRefs%3Dtrue-,cacheSeconds,-No}
     */
    cacheSeconds?: number;
    /**
     * Builder.io uses stale-while-revalidate caching at the CDN level. This means we always serve
     * from edge cache and update caches in the background for maximum possible performance. This also
     * means that the more frequently content is requested, the more fresh it will be. The longest we
     * will ever hold something in stale cache is 1 day by default, and you can set this to be shorter
     * yourself as well. We suggest keeping this high unless you have content that must change rapidly
     * and gets very little traffic.
     *
     * @see {@link https://www.fastly.com/blog/prevent-application-network-instability-serve-stale-content}
     */
    staleCacheSeconds?: number;
    /**
     * Maximum number of results to return. Defaults to `1`.
     */
    limit?: number;
    /**
     * Mongodb style query of your data. E.g.:
     *
     * ```
     * &query.data.id=abc123
     * &query.data.myCustomField=someValue
     * &query.data.someNumber.$ne=20
     * ```
     *
     * See more info on MongoDB's query operators and format.
     *
     * @see {@link https://docs.mongodb.com/manual/reference/operator/query/}
     */
    query?: any;
    /**
     * Bust through all caches. Not recommended for production (for performance),
     * but can be useful for development and static builds (so the static site has
     * fully fresh / up to date content)
     */
    cachebust?: boolean;
    /**
     * Convert any visual builder content to HTML.
     *
     * This will be on data.html of the response's content entry object json.
     */
    prerender?: boolean;
    /**
     * Extract any styles to a separate css property when generating HTML.
     */
    extractCss?: boolean;
    /**
     * Pagination results offset. Defaults to zero.
     */
    offset?: number;
    /**
     * @package
     *
     * `BuilderContent` to render instead of fetching.
     */
    initialContent?: any;
    /**
     * The name of the model to fetch content for.
     */
    model?: string;
    /**
     * Set to `false` to not cache responses when running on the client.
     */
    cache?: boolean;
    /**
     * @package
     *
     * Indicate that the fetch request is for preview purposes.
     */
    preview?: boolean;
    /**
     * Specific content entry ID to fetch.
     */
    entry?: string;
    /**
     * @package
     * @deprecated
     */
    alias?: string;
    fields?: string;
    /**
     * Omit only these fields.
     *
     * @example
     * ```
     * &omit=data.bigField,data.blocks
     * ```
     */
    omit?: string;
    key?: string;
    /**
     * @package
     *
     * Affects HTML generation for specific targets.
     */
    format?: 'amp' | 'email' | 'html' | 'react' | 'solid';
    /** @deprecated */
    noWrap?: true;
    /**
     * @package
     *
     * Specific string to use for cache busting. e.g. every time we generate
     * HTML we generate a rev (a revision ID) and we send that with each request
     * on the client, such that if we generate new server HTML we get a new rev
     * and we use that to bust the cache because even though the content ID may
     * be the same, it could be an updated version of this content that needs to
     * be fresh.
     */
    rev?: string;
    /**
     * @package
     *
     * Tell the API that when generating HTML to generate it in static mode for
     * a/b tests instead of the older way we did this
     */
    static?: boolean;
    /**
     * Additional query params of the Content API to send.
     */
    options?: {
        [key: string]: any;
    };
    /**
     * @package
     *
     * Don't listen to updates in the editor - this is useful for embedded
     * symbols so they don't accidentally listen to messages as you are editing
     * content thinking they should updates when they actually shouldn't.
     */
    noEditorUpdates?: boolean;
}

My function:

Blog.getInitialProps = async ({ req, res, asPath, query }) => {
  const page = Number(query.page) || 0
  const articles = await builder.getAll('blog-article', {
    req,
    res,
    options: { includeRefs: true },
    omit: 'data.blocks',
    sort: {         //THIS IS NOT WORKING
    "sort.data.date": -1
    },
    limit: articlesPerPage,
    offset: page * articlesPerPage,
  })
  console.log(articles)

  return { articles }
}

But this is not working, I got my articles in the wrong order. Can you please check this out, or how can I sort my articles by the date property?

Thank you in advanced!

Hey @radikris It hink you have sort in there twice…you should only need:

sort: {
   "data.date": -1
}

Try that and see if it works

I got that from the api-explorer:

And no, not working, I copied the jsx properties in my previous answer, and it looks like it does not have the sort parameter there?

I believe it is just a different syntax. Passing in "sort.data.date"is same as sort: { data: { date: -1 }}} is same as sort: { "data.date": -1}, etc

As to the other inputs, what are you passing in as req, res? Try removing your other inputs to see if you can get the sort working first, something like ;

const articles = await builder.getAll('blog-article', {
    options: { includeRefs: true },
    omit: 'data.blocks',
    sort: {        
    'data.date': -1
    }
  })

Also make sure your results all have a data.date value to sort off of. I tested on my local with a similar model and was able to see sorted results, so maybe there is something else in your code you are passing in that is breaking the query?

hmm strange:

This is my query:


Blog.getInitialProps = async ({ req, res, asPath, query }) => {
  const page = Number(query.page) || 0
  const articles = await builder.getAll('blog-article', {
    options: { includeRefs: true },
    omit: 'data.blocks',
    sort: {
      'data.date': -1,
    },
    limit: articlesPerPage,
    offset: page * articlesPerPage,
  })

  console.log(articles)

  return { articles }
}

And here are the results with data.date: -1, and +1

for both data.date: -1 or +1 the result is same, which means the sorting is not working properly:

[
  {
    createdBy: 'uw0UJ7VD7hS21zDPkekcB9zZgAD3',
    createdDate: 1647334769661,
    data: {
      category: 'Developers',
      date: 1646089200000,
      description: '<p>Ide meg mar a josszabb description jonne mint a blogpost ezaz hosszabb blog gyere</p>',
      descriptionpreview: 'You can create many blogposts in builder io barion yeah',
      image: 'https://cdn.builder.io/api/v1/image/assets%2F32f148657e2646be8562eb4e6ebfa190%2F9bf4c59b9a8c44b2a934e9e593fd6657',
      title: 'How to create blog in builder',
      url: 'blog4'
    },
    firstPublished: 1647333997595,
    id: '1ce59a2b8c3e4ac4930aa1106e5abbda',
    lastUpdated: 1647363106455,
    lastUpdatedBy: 'uw0UJ7VD7hS21zDPkekcB9zZgAD3',
    meta: {
      hasLinks: false,
      kind: 'component',
      originalContentId: '00cdc2fde718483a90856ae7b787d7e8',
      winningTest: null
    },
    modelId: 'aba995a993e44c99851f39b55390c43b',
    name: 'blog4',
    published: 'published',
    query: [],
    testRatio: 1,
    variations: {},
    screenshot: 'https://cdn.builder.io/api/v1/image/assets%2F32f148657e2646be8562eb4e6ebfa190%2F244a3558eead4ec4b38b125ddb566945',
    rev: '0l8msglh5rg'
  },
  {
    createdBy: 'uw0UJ7VD7hS21zDPkekcB9zZgAD3',
    createdDate: 1647334700438,
    data: {
      category: 'Hirek',
      date: 1647126000000,
      description: '<p>Ide meg mar a josszabb description jonne mint a blogpost ezaz hosszabb blog gyere</p>',
      descriptionpreview: 'Harmdik descrisadada a das aasdasdds sda',
      image: 'https://cdn.builder.io/api/v1/image/assets%2F32f148657e2646be8562eb4e6ebfa190%2F9bf4c59b9a8c44b2a934e9e593fd6657',
      title: 'Harmdik blog',
      url: 'blog3'
    },
    firstPublished: 1647333997595,
    id: '00cdc2fde718483a90856ae7b787d7e8',
    lastUpdated: 1647359654370,
    lastUpdatedBy: 'uw0UJ7VD7hS21zDPkekcB9zZgAD3',
    meta: {
      kind: 'component',
      originalContentId: 'e97721f4a25e426e86c74e0d91b7dae0',
      winningTest: null
    },
    modelId: 'aba995a993e44c99851f39b55390c43b',
    name: 'blog3',
    published: 'published',
    query: [],
    testRatio: 1,
    variations: {},
    rev: '0l8msglh5rg'
  },
  {
    createdBy: 'uw0UJ7VD7hS21zDPkekcB9zZgAD3',
    createdDate: 1647334099630,
    data: {
      category: 'Hirek',
      date: 1647212400000,
      description: '<p>Ide meg mar a josszabb description jonne mint a blogpost ezaz hosszabb blog gyere</p>',
      descriptionpreview: 'Ide valami masodik lorem ipsumsdas :) :D :(',
      image: 'https://cdn.builder.io/api/v1/image/assets%2F32f148657e2646be8562eb4e6ebfa190%2F329f92b55e0f4c799da59b4168d26981',
      title: 'Masodik blog',
      url: 'blog2'
    },
    firstPublished: 1647333997595,
    id: 'e97721f4a25e426e86c74e0d91b7dae0',
    lastUpdated: 1647359643892,
    lastUpdatedBy: 'uw0UJ7VD7hS21zDPkekcB9zZgAD3',
    meta: {
      kind: 'component',
      originalContentId: '9ea394d394e4497d99dbeccd6296e377',
      winningTest: null
    },
    modelId: 'aba995a993e44c99851f39b55390c43b',
    name: 'blog2',
    published: 'published',
    query: [],
    testRatio: 1,
    variations: {},
    rev: '0l8msglh5rg'
  },
  {
    createdBy: 'uw0UJ7VD7hS21zDPkekcB9zZgAD3',
    createdDate: 1647333965365,
    data: {
      category: 'Hirek',
      date: 1647385200000,
      description: '<p>Ide meg mar a josszabb description jonne mint a blogpost ezaz hosszabb blog gyere</p>',
      descriptionpreview: 'Ide valami rovidebb description',
      image: 'https://cdn.builder.io/api/v1/image/assets%2F32f148657e2646be8562eb4e6ebfa190%2Ff623f178d8544427b1ded2f7659173f6',
      title: 'Elso blog',
      url: 'blog1'
    },
    id: '9ea394d394e4497d99dbeccd6296e377',
    lastUpdatedBy: 'uw0UJ7VD7hS21zDPkekcB9zZgAD3',
    meta: { kind: 'component' },
    modelId: 'aba995a993e44c99851f39b55390c43b',
    name: 'blog1',
    published: 'published',
    query: [],
    testRatio: 1,
    variations: {},
    lastUpdated: 1647336257023,
    firstPublished: 1647333997595,
    rev: '0l8msglh5rg'
  }
]

What could be the issue?

@radikris apologies, looks like my test environment was already pre-sorted which threw me off :sweat_smile:

You actually need to include the sort as part of options, please try this:

Blog.getInitialProps = async ({ req, res, asPath, query }) => {
  const page = Number(query.page) || 0
  const articles = await builder.getAll('blog-article', {
    options: {
        includeRefs: true,
        sort: {
            'data.date': -1
        }   
     },
    omit: 'data.blocks',
    limit: articlesPerPage,
    offset: page * articlesPerPage,
  })
  console.log(articles)
  return { articles }
}

@TimG
I used it via api query but I can’t seem to get the sorted data by datePublished. Did I query it wrong? Is +1 ascending and -1 descending?

https://cdn.builder.io/api/v2/content/blog?apiKey={somekeys}&offset=${blogOffset}&limit=${blogLimit}&sort.data.datePublished=-1

Api-explorer and from code the query is not the same:

curl -X GET “https://cdn.builder.io/api/v2/content/blog-article?apiKey=key&limit=10&sort.data.customField=1&sort.createdDate=-1” -H “accept: /”

1 Like

@manuel I am assuming datePublished is a custom field on your blog model? If so then that should work.

You can see an example of sorting on a custom field value here:
https://cdn.builder.io/api/v2/content/article?apiKey=e37b966ec695434bb21e97442a4a9f46&offset=0&limit=100&sort.data.title=1&fields=data.title

Based on this your query should be correct, if you have a link to tyour model or your API key I can further look into it

@TimG Here’s the sample response from the model api

[
    {
        "createdBy": "j3LoMNP1ItQjaCXhZlyaPWctHQJ3",
        "createdDate": 1648691011401,
        "data": {
            "category": "LEADERSHIP",
            "content": "<p>&nbsp;John 11:35: “Jesus wept.\"</p><p>Romans 12:15: “…weep with those who weep.”</p><h5>Weeping is a part of pastoring. At least, it should be.</h5><p>&nbsp;</p><p>I’ve described this before in various places. When you respond affirmatively to the call to pastor a local church, you are knit together with a community. The experience of this is that you are constantly weeping; weeping because there is invariably always someone within the community with cause to weep. It’s a high privilege to share in this. After all, Jesus wept.</p><p>&nbsp;</p><p>Now, this is generally offset by the preceding part of Romans 12:15: ‘Rejoice with those who rejoice…’ You are constantly rejoicing; rejoicing, because there is invariably always someone within the community with cause to rejoice. It’s a great joy to share in this. There are babies and marriages and baptisms and progress. Life is beautiful.</p><p>And life is hard.</p><p>“Rejoice with those who rejoice, weep with those who weep.” This amounts to what Paul says next- “Live in harmony with one another…” (This, by the way, is not just addressed to Pastors. It’s addressed to all believers.)</p><p>&nbsp;</p><p>What does faith sound like when we have cause to weep? What does faith say through tears?</p><p>We surely know, by now, that it doesn’t sound like - “THERE’S NO CAUSE TO WEEP”.</p><h5>Faith doesn’t seek to ‘bury one’s head in the sand’, ‘jam one’s fingers in one’s ears' and pretend that grim shadows and circumstances don’t exist. That’s not faith - that’s delusion.</h5><p>&nbsp;</p><p>Faith says ‘…but God.’ ‘But God will help’. ‘But God will make a way.’ ‘But God will vindicate / deliver / heal / resurrect.’ ‘He will somehow make things work together for my good.’</p><h5>Faith acknowledges the difficulty, but then professes a supreme faith in the greater reality.</h5><p>&nbsp;</p><p>Romans 8:31-35, 37-39:</p><p>“What then shall we say to these things? If God is for us, who can be against us? He who did not spare his own Son but gave him up for us all, how will he not also with him graciously give us all things? Who shall bring any charge against God's elect? It is God who justifies. Who is to condemn? Christ Jesus is the one who died- more than that, who was raised- who is at the right hand of God, who indeed is interceding for us. Who shall separate us from the love of Christ? Shall tribulation, or distress, or persecution, or famine, or nakedness, or danger, or sword?... No, in all these things we are more than conquerors through him who loved us. For I am sure that neither death nor life, nor angels nor rulers, nor things present nor things to come, nor powers, nor height nor depth, nor anything else in all creation, will be able to separate us from the love of God in Christ Jesus our Lord.’</p><p>That’s probably more like what faith says through tears.</p><p>&nbsp;</p><p>I guess my concern today though is when does faith say that?</p><h4>If we take the passage that I quoted just above (Romans 8:31-35, 37-39) as a ‘faith statement’, when should we interrupt lament with a ‘BUT’?</h4><p>My contention is that many of us do it too soon. For sure, some do it not at all, or not soon enough. But perhaps some can relate to me in this- I want to skip the weeping and get to the faith talking. I am, after all, a Pentecostal.</p><p>But maybe I need to learn from Jesus.</p><p>&nbsp;</p><p>When Jesus wept in John 11:35, He was omnisciently and omnipotently in control. He knows that while Lazarus had died, He was imminently going to raise him from the dead. But before He does it, He took a moment to be ‘deeply moved in his spirit’; to be ‘greatly troubled’; and ‘to weep’. He took a moment to empathize; to grieve; to lament.</p><p>&nbsp;</p><p>Yes, Jesus will, in time, liberate us from all pain and from all suffering. But first, He identifies with us in our pain and in our suffering. While it’s amazing to me that God is able to raise from the dead, it’s even more amazing to me that before He does, He takes a moment to weep.</p><p>&nbsp;</p><p>Right now, life feels especially hard. Life feels especially heavy. There are still moments of rejoicing, but there’s a lot of weeping. That’s what I signed up for though…not just by saying ‘yes’ to being a pastor. I signed up for this when I said ‘yes’ to following Jesus - the one who weeps over people that He will imminently raise and over cities that He will imminently restore.</p><h5>What does faith sound like through tears? Maybe it does still sound like, 'life is hard … BUT GOD,' but with more time (and less words) spent in the ‘…’</h5><p>&nbsp;</p>",
            "datePublished": "Fri Apr 01 2022 12:00:00 GMT+1100 (Australian Eastern Daylight Time)",
            "image": "https://cdn.builder.io/api/v1/image/assets%2F5cde70e5453e4d12ba7e8c55a728859d%2F005634dabffb4960a2012ea9385ddba7",
            "publishedBy": "Steve Burgess",
            "publishedByReference": {
                "@type": "@builder.io/core:Reference",
                "id": "",
                "model": ""
            },
            "subTitle": "Weeping is a part of pastoring. At least, it should be.",
            "title": "Jesus Wept"
        },
        "id": "0715b1f841ee4d26af94bb9c3529098f",
        "lastUpdatedBy": "j3LoMNP1ItQjaCXhZlyaPWctHQJ3",
        "meta": {
            "kind": "data"
        },
        "modelId": "c7cee3ff8a9947b4b66e45faf7530cdc",
        "name": "Jesus Wept",
        "published": "published",
        "query": [],
        "testRatio": 1,
        "variations": {},
        "lastUpdated": 1648693005015,
        "firstPublished": 1648691334061,
        "rev": "89ih87qfejh"
    },
    {
        "createdBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "createdDate": 1647169410984,
        "data": {
            "category": "DEVOTIONALS",
            "content": "<drance than an asset in terms of the kingdom. The better I think I am, the less I magnify God. Our goal is not the absence of weakness, but magnification of God’s strength</p><p><br></p>",
            "datePublished": "Fri Apr 08 2016 00:00:00 GMT+0800 (Philippine Standard Time)",
            "publishedBy": "SAM PICKEN",
            "publishedByReference": {
                "@type": "@builder.io/core:Reference",
                "id": "0538bd7ea4424920a00fc0efb6856306",
                "model": "author"
            },
            "subTitle": "Check in every Monday for a new devotional from one of our pastors from around the globe. This week we have an incredible word on #power from Ps Sam Picken from C3 Toronto",
            "title": "Power"
        },
        "id": "3efdd0932e894198b6f0946f5a5a2d7f",
        "lastUpdatedBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "meta": {
            "kind": "data"
        },
        "modelId": "c7cee3ff8a9947b4b66e45faf7530cdc",
        "name": "Power",
        "published": "published",
        "query": [],
        "testRatio": 1,
        "variations": {},
        "lastUpdated": 1647169520760,
        "rev": "89ih87qfejh"
    },
    {
        "createdBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "createdDate": 1647170213282,
        "data": {
            "category": "DEVOTIONALS",
            "content": "<p><strong>ong because you know who he is and what he’s done for you.</p><p><br></p>",
            "datePublished": "Fri Apr 08 2016 00:00:00 GMT+0800 (Philippine Standard Time)",
            "publishedBy": "PHIL & DENISE BEUCHLER",
            "publishedByReference": {
                "@type": "@builder.io/core:Reference",
                "id": "22b705b6b4a849368966dc5452e3511f",
                "model": "author"
            },
            "subTitle": "Our July devotionals are coming to us from Phil & Denise Beuchler, senior pastors of C3 Long Island! Check in every Monday for a new devotional, and read their devotional below!",
            "title": "Relationship"
        },
        "id": "443caeab66104b1ca31d9be396ad4395",
        "lastUpdatedBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "meta": {
            "kind": "data"
        },
        "modelId": "c7cee3ff8a9947b4b66e45faf7530cdc",
        "name": "Relationship",
        "published": "published",
        "query": [],
        "testRatio": 1,
        "variations": {},
        "lastUpdated": 1647170347428,
        "rev": "89ih87qfejh"
    },
    {
        "createdBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "createdDate": 1647748394971,
        "data": {
            "category": "LEADERSHIP",
            "content": "<p>Vision is so import",
            "datePublished": "Fri Apr 15 2016 00:00:00 GMT+0800 (Philippine Standard Time)",
            "publishedBy": "JOHN PEARCE",
            "publishedByReference": {
                "@type": "@builder.io/core:Reference",
                "id": "6f127e56ca404492ac2344f9656e5820",
                "model": "author"
            },
            "subTitle": "WHERE THERE IS NO VISION, THE PEOPLE PERISH…” Proverbs 29:18",
            "title": "Vision"
        },
        "id": "8b961e8c911240a28a83e9e4792a91e7",
        "lastUpdatedBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "meta": {
            "kind": "data"
        },
        "modelId": "c7cee3ff8a9947b4b66e45faf7530cdc",
        "name": "Vision",
        "published": "published",
        "query": [],
        "testRatio": 1,
        "variations": {},
        "lastUpdated": 1647748545297,
        "firstPublished": 1647748545294,
        "rev": "89ih87qfejh"
    },
    {
        "createdBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "createdDate": 1647080735281,
        "data": {
            "category": "XPRESS",
            "content": "<h5><strong><em>“W",
            "datePublished": "Fri Aug 09 2019 00:00:00 GMT+0800 (Philippine Standard Time)",
            "image": "https://cdn.builder.io/api/v1/image/assets%2F5cde70e5453e4d12ba7e8c55a728859d%2F0bea375bf6eb4ad28f8bb4bd9f63137d",
            "publishedBy": " ADAM & KEIRA SMALLCOMBE",
            "publishedByReference": {
                "@type": "@builder.io/core:Reference",
                "id": "87a3575f20dd464ab31a3e25ddbb3d5b",
                "model": "author"
            },
            "subTitle": "“We’re just checking you out…” is one of the most common phrases church planters hear when starting a church.",
            "title": "Building A Church You Actually Like"
        },
        "id": "9b38400b6be64c5e812d9e2937661798",
        "lastUpdatedBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "meta": {
            "kind": "data"
        },
        "modelId": "c7cee3ff8a9947b4b66e45faf7530cdc",
        "name": "Building A Church You Actually Like",
        "published": "published",
        "query": [],
        "testRatio": 1,
        "variations": {},
        "lastUpdated": 1647081610523,
        "firstPublished": 1647081610520,
        "rev": "89ih87qfejh"
    },
    {
        "createdBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "createdDate": 1647323839752,
        "data": {
            "category": "XPRE><br></p>",
            "datePublished": "Fri Aug 09 2019 00:00:00 GMT+0800 (Philippine Standard Time)",
            "image": "https://cdn.builder.io/api/v1/image/assets%2F5cde70e5453e4d12ba7e8c55a728859d%2F168a8c41f20a4ed6a7013abe3b4ecf89",
            "publishedBy": "PAT ANTCLIFF",
            "publishedByReference": {
                "@type": "@builder.io/core:Reference",
                "id": "92cbda0fe38a4594885559b655b79fda",
                "model": "author"
            },
            "subTitle": "The to be selected and to thrive as a location pastor, and we will focus upon those here.",
            "title": "Choosing Location Pastors"
        },
        "id": "5059d5880e3945fb94b2991f7c7115a3",
        "lastUpdatedBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "meta": {
            "kind": "data"
        },
        "modelId": "c7cee3ff8a9947b4b66e45faf7530cdc",
        "name": "Choosing Location Pastors",
        "published": "published",
        "query": [],
        "testRatio": 1,
        "variations": {},
        "lastUpdated": 1647323990394,
        "firstPublished": 1647323990391,
        "rev": "89ih87qfejh"
    },
    {
        "createdBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "createdDate": 1647570235927,
        "data": {
            "category": "XPRESS-WHY",
            "content": "<p>We",
            "datePublished": "Fri Aug 09 2019 00:00:00 GMT+0800 (Philippine Standard Time)",
            "image": "https://cdn.builder.io/api/v1/image/assets%2F5cde70e5453e4d12ba7e8c55a728859d%2Ffbc4dfb27bf9495e9aa29f671f12456d",
            "publishedBy": "PHIL BUECHLER",
            "publishedByReference": {
                "@type": "@builder.io/core:Reference",
                "id": "d645b1802e6c4440bb97350fa9cde775",
                "model": "author"
            },
            "subTitle": "Faith in ministry includes setting goals so incredibly bold that you’re bound to fail unless God moves in a miraculous way. ",
            "title": "The Biblical Basis Of Expansion Part II"
        },
        "id": "59788e0fa5f8409abd2297cbe0ac84e5",
        "lastUpdatedBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "meta": {
            "kind": "data"
        },
        "modelId": "c7cee3ff8a9947b4b66e45faf7530cdc",
        "name": "The Biblical Basis Of Expansion Part II",
        "published": "published",
        "query": [],
        "testRatio": 1,
        "variations": {},
        "lastUpdated": 1647769967056,
        "firstPublished": 1647570340861,
        "rev": "89ih87qfejh"
    },
    {
        "createdBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "createdDate": 1647570630581,
        "data": {
            "category": "XPRESS-WHY",
            "content": "<p>In this mandate we see mirrored, or, more properly, fulfilled, the creative and obligatory decree of Genesis 1:2&amp;H Academic, 2016), 42</p>",
            "datePublished": "Fri Aug 09 2019 00:00:00 GMT+0800 (Philippine Standard Time)",
            "image": "https://cdn.builder.io/api/v1/image/assets%2F5cde70e5453e4d12ba7e8c55a728859d%2F266143289b054ee7a958af3adaa97aec",
            "publishedBy": "SIMON MCLNTYRE",
            "publishedByReference": {
                "@type": "@builder.io/core:Reference",
                "id": "cc379e8fd74b40c88a403b8533c20017",
                "model": "author"
            },
            "subTitle": "Planting churches is not an option for the adventurous – it is a mandate for all of God’s church.",
            "title": "The Biblical Basis Of Expansion Part I"
        },
        "id": "6456a3cec4044c9197862c26faa7719b",
        "lastUpdatedBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "meta": {
            "kind": "data"
        },
        "modelId": "c7cee3ff8a9947b4b66e45faf7530cdc",
        "name": "The Biblical Basis Of Expansion Part I",
        "published": "published",
        "query": [],
        "testRatio": 1,
        "variations": {},
        "lastUpdated": 1647769952136,
        "firstPublished": 1647570935625,
        "rev": "89ih87qfejh"
    },
    {
        "createdBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "createdDate": 1647568854997,
        "data": {
            "category": "LEADERSHIP",
            "content": "<p>And wp>",
            "datePublished": "Fri Aug 18 2017 00:00:00 GMT+0800 (Philippine Standard Time)",
            "image": "https://cdn.builder.io/api/v1/image/assets%2F5cde70e5453e4d12ba7e8c55a728859d%2Ff47cb01bf6214d1aab459d8749cf3dd7",
            "publishedBy": "VALERIE MCLNTYRE",
            "publishedByReference": {
                "@type": "@builder.io/core:Reference",
                "id": "2ce81db7e165428da97439bdc41b7357",
                "model": "author"
            },
            "subTitle": "We often hear a lot about our spiritual life, how to grow it, how to revive it, and the importance of spending time in prayer and the Word.",
            "title": "Soul Food"
        },
        "id": "c8b1834d33c04481bd25c82a0a89f378",
        "lastUpdatedBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "meta": {
            "kind": "data"
        },
        "modelId": "c7cee3ff8a9947b4b66e45faf7530cdc",
        "name": "Soul Food",
        "published": "published",
        "query": [],
        "testRatio": 1,
        "variations": {},
        "lastUpdated": 1647568994390,
        "firstPublished": 1647568994389,
        "rev": "89ih87qfejh"
    },
    {
        "createdBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "createdDate": 1647698255436,
        "data": {
            "category": "DEVOTIONALS",
            "content": "<p>The wise men fo",
            "image": "https://cdn.builder.io/api/v1/image/assets%2F5cde70e5453e4d12ba7e8c55a728859d%2Ff9f40bea1e954d50bf250d4473d7c04a",
            "publishedBy": "THIERRY MOEHR",
            "publishedByReference": {
                "@type": "@builder.io/core:Reference",
                "id": "b7b13fb87d8d46ff804040eba5269794",
                "model": "author"
            },
            "subTitle": "“And r treasures, they offered him gifts, gold and frankincense and myrrh.”",
            "title": "Twelve Days of Christmas: Day Three"
        },
        "id": "38ea0fc208064bc8864509baecf5e8b4",
        "lastUpdatedBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "meta": {
            "kind": "data"
        },
        "modelId": "c7cee3ff8a9947b4b66e45faf7530cdc",
        "name": "Twelve Days of Christmas: Day Three",
        "published": "published",
        "query": [],
        "testRatio": 1,
        "variations": {},
        "lastUpdated": 1647698528640,
        "firstPublished": 1647698528637,
        "rev": "89ih87qfejh"
    },
    {
        "createdBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "createdDate": 1647747887762,
        "data": {
            "category": "DEVOTIONALS",
            "content": "<p>Baffled d whom the whole universe realigns to glorify.</p>",
            "datePublished": "Fri Dec 15 2017 00:00:00 GMT+0800 (Philippine Standard Time)",
            "image": "https://cdn.builder.io/api/v1/image/assets%2F5cde70e5453e4d12ba7e8c55a728859d%2Fd9ccd871c1b74d70a69572dfc17d1533",
            "publishedBy": "JOEL BURDEN",
            "publishedByReference": {
                "@type": "@builder.io/core:Reference",
                "id": "6cecb0a1f6c24ce7b87cc81d62ee799c",
                "model": "author"
            },
            "subTitle": "There’s and.",
            "title": "Twelve Days of Christmas: Day Two"
        },
        "id": "1ede434f872f458b828843485e1a8da9",
        "lastUpdatedBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "meta": {
            "kind": "data"
        },
        "modelId": "c7cee3ff8a9947b4b66e45faf7530cdc",
        "name": "Twelve Days of Christmas: Day Two",
        "published": "published",
        "query": [],
        "testRatio": 1,
        "variations": {},
        "lastUpdated": 1647748228511,
        "firstPublished": 1647748228506,
        "rev": "89ih87qfejh"
    },
    {
        "createdBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "createdDate": 1647066273089,
        "data": {
            "category": "DEVOTIONALS",
            "content": "<p><span styl55, 255); font-size: 14px; color: inherit;\">here</a></p>",
            "datePublished": "Fri Dec 16 2016 00:00:00 GMT+0800 (Philippine Standard Time)",
            "image": "https://cdn.builder.io/api/v1/image/assets%2F5cde70e5453e4d12ba7e8c55a728859d%2Ffeb83988abeb426c96b361bef4111047",
            "publishedBy": "JOANNA MIKAC",
            "publishedByReference": {
                "@type": "@builder.io/core:Reference",
                "id": "bb7f82b1329849de9a0f27ad0f565135",
                "model": "author"
            },
            "subTitle": "The 12 Days of Christmas // A twelve day guided devotional bringing together a rich collection of family voices to comment on the spirit of the season – what it means to us as a community, and the world at large.",
            "title": "12 Days of Christmas Devotional"
        },
        "id": "2b4d6e566b484d8e8c0bfe9309131adc",
        "lastUpdatedBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "meta": {
            "kind": "data"
        },
        "modelId": "c7cee3ff8a9947b4b66e45faf7530cdc",
        "name": "12 Days of Christmas Devotional",
        "published": "published",
        "query": [],
        "testRatio": 1,
        "variations": {},
        "lastUpdated": 1647074017777,
        "firstPublished": 1647074017776,
        "rev": "89ih87qfejh"
    },
    {
        "createdBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "createdDate": 1647765632647,
        "data": {
            "category": "DEVOTIONALS",
            "content": "<p>This verse of Scripture fro",
            "datePublished": "Fri Dec 22 2017 00:00:00 GMT+0800 (Philippine Standard Time)",
            "image": "https://cdn.builder.io/api/v1/image/assets%2F5cde70e5453e4d12ba7e8c55a728859d%2F5d8db420d0ba439081d8000e7fc42a91",
            "publishedBy": "SARAH PERCY",
            "publishedByReference": {
                "@type": "@builder.io/core:Reference",
                "id": "5a480b1b878e4958aa33e4edb75dc9eb",
                "model": "author"
            },
            "subTitle": "“Where is he who has been born King of the Jews? For we saw his star when it rose and have come to worship him.”  Matt 2 v 2",
            "title": "Twelve Days of Christmas: Day Nine"
        },
        "id": "2c91594274374fc584d1196a2398736a",
        "lastUpdatedBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "meta": {
            "kind": "data"
        },
        "modelId": "c7cee3ff8a9947b4b66e45faf7530cdc",
        "name": "Twelve Days of Christmas: Day Nine",
        "published": "published",
        "query": [],
        "testRatio": 1,
        "variations": {},
        "lastUpdated": 1647765791825,
        "firstPublished": 1647765791823,
        "rev": "89ih87qfejh"
    },
    {
        "createdBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "createdDate": 1647175766837,
        "data": {
            "category": "LEADERSHIP",
            "content": "<p>I loved how Pastor Phil helped remind us in the C3 Global Christmas Video that success in the Christian life is best defined as fruitfulness and I wholeheartedly agree. I also know that there are competing bids for our hearts w",
            "datePublished": "Fri Feb 09 2018 00:00:00 GMT+0800 (Philippine Standard Time)",
            "image": "https://cdn.builder.io/api/v1/image/assets%2F5cde70e5453e4d12ba7e8c55a728859d%2F8bdf64113dab4725add8673e06a0a7a9",
            "publishedBy": "LORNE TEBBUT",
            "publishedByReference": {
                "@type": "@builder.io/core:Reference",
                "id": "cf83bd55e2e44d3497336358bcce862a",
                "model": "author"
            },
            "subTitle": "I fe are some thoughts “Kingdom Connections; So Far.”",
            "title": "Kingdom Connections"
        },
        "id": "b1c26989703b423b94189addf2b83f97",
        "lastUpdatedBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "meta": {
            "kind": "data"
        },
        "modelId": "c7cee3ff8a9947b4b66e45faf7530cdc",
        "name": "Kingdom Connections",
        "published": "published",
        "query": [],
        "testRatio": 1,
        "variations": {},
        "lastUpdated": 1647176047826,
        "firstPublished": 1647176047824,
        "rev": "89ih87qfejh"
    },
    {
        "createdBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "createdDate": 1647083113860,
        "data": {
            "category": "DEVOTIONALS",
            "content": "<p><strong><em>Psalut it: tn.</p><p><br></p>",
            "datePublished": "Fri Jan 08 2016 00:00:00 GMT+0800 (Philippine Standard Time)",
            "publishedBy": "LARS & MEGAN HALVORSEN",
            "publishedByReference": {
                "@type": "@builder.io/core:Reference",
                "id": "0015ba1eeda74295848b34231d169dea",
                "model": "author"
            },
            "subTitle": "Lars & Megan Halvorsen are the senior pastors of C3 Darwin, and are checking in this week with a devotional for you! Read Lars’ devotional below:",
            "title": "Attention"
        },
        "id": "988b4ab3b3d74cc097309a88352ea4fc",
        "lastUpdatedBy": "q5Xfb2Iq9DWj6lYGGRU3C29BSe63",
        "meta": {
            "kind": "data"
        },
        "modelId": "c7cee3ff8a9947b4b66e45faf7530cdc",
        "name": "Attention",
        "published": "published",
        "query": [],
        "testRatio": 1,
        "variations": {},
        "lastUpdated": 1647083663112,
        "rev": "89ih87qfejh"
    }
]
1 Like

Ah, yes, @manuel I see the issue. You have the datePublished as an input type Date, which is saved in database as a string (ex: Fri Apr 01 2022 12:00:00 GMT+1100 (Australian Eastern Daylight Time))

So the sort in your API call is working, but it is sorting by alphabetical order based on the date string. You can see it a little easier if you add the fields param to only show name and datePublished: https://cdn.builder.io/api/v2/content/blog?apiKey=5cde70e5453e4d12ba7e8c55a728859d&offset=0&limit=40&sort.data.datePublished=+1&fields=name,data.datePublished

I think what you actually want is the input type of timestamp which is more easily queryable.
Screen Shot 2022-04-02 at 7.44.59 AM

You can keep both fields to have a legible date, and the queryable timestamp, OR, if you ever want to convert the timestamp to a readable date in code, you can do so with:

const date = new Date(timestampValue)

You will need to re-save each data model with the new timestamp value to be able to query it from the API. Hope that helps!

1 Like

@TimG Awesome, thank you so much! I think this will fix my sorting issue. Will try this out!

1 Like

@TimG It worked. Thanks a lot!

1 Like

Just ran into this and would argue it’s a bug that should be fixed. Why would you ever want a custom Date field to be sorted as a string?

hi @martypdx , the use case for timestamp and Date are different, which we have tried to call out in the input type description. I will bring back to our product team to see if we can improve that text to make things even more clear…we appreciate the feedback!

Ah, I went back to the docs and see what you mean. Timestamp still presents to the CMS user as a date. If I change the type on the model, will the data convert?

@martypdx it will not automatically convert, but if you go into a content entry and re-publish it will convert it to the new input type selected

1 Like

Hi @TimG, the sorting option seems to no longer be working for me. Is there an update to the syntax? Is what I’ve been using already deprecated?

Here’s my api call

https://cdn.builder.io/api/v2/content/blog?apiKey=5cde70e5453e4d12ba7e8c55a728859d&limit=${blogLimit}&sort.data.datePublished=-1&query.data.category.$regex=${category}

Hey @manuel we did not make any changes to our v2 of the sort, so there shouldnt be any change… but let me look into it a bit for you.

In the meantime however, we did launch a new updated version of our content API: Content API Versions - Builder.io

And that has the correct sorting when updated to hit that URL

https://cdn.builder.io/api/v3/content/blog?apiKey=5cde70e5453e4d12ba7e8c55a728859d&limit=100&sort.data.datePublished=1&query.data.category.$regex=LEADERSHIP&fields=data.datePublished,name

Awesome! /v3 is working for me. Thank you @TimG . It’s kind of weird why the /v2 isn’t working tho