Search Storyblok's Documentation
  1. Sorting by Story Object Property

Sorting by Story Object Property

Stories can be sorted based on custom or predefined properties by employing the sort_by parameter, with field:asc or field:desc as values, allowing for ascending or descending order, respectively.

  • sort_by=name:asc

    Sort by the story object property name

  • sort_by=position:desc

    Sort stories in the same order as determined in the CMS

  • sort_by=first_published_at:desc

    Sort by the Story object property first_published_at

  • Example Request and Response

    The following example demonstrates how to sort stories in descending order by their first published date.

    Request
    curl "https://api.storyblok.com/v2/cdn/stories?token=krcV6QGxWORpYLUWt12xKQtt&sort_by=first_published_at:desc" \
      -X GET \
      -H "Accept: application/json" \
      -H "Content-Type: application/json"
    Request
    // Using the Universal JavaScript Client:
    // https://github.com/storyblok/storyblok-js-client
    Storyblok.get('cdn/stories', {
      "sort_by": "first_published_at:desc"
    })
      .then(response => {
        console.log(response)
      }).catch(error => { 
        console.log(error)
      })
    Request
    $client = new \Storyblok\Client('YOUR_STORYBLOK_SPACE_ACCESS_TOKEN');
    
    $client->getStories([
      "sort_by" =>  "first_published_at => desc"
    ])->getBody();
    Request
    require 'storyblok'
    client = Storyblok::Client.new(oauth_token: 'YOUR_OAUTH_TOKEN')
    
    client.stories({:params => {
      "sort_by" =>  "first_published_at => desc"
    }})
    Request
    HttpResponse<String> response = Unirest.get("https://api.storyblok.com/v2/cdn/stories?token=krcV6QGxWORpYLUWt12xKQtt&sort_by=first_published_at:desc")
      .asString();
    Request
    var client = new RestClient("https://api.storyblok.com/v2/cdn/stories?token=krcV6QGxWORpYLUWt12xKQtt&sort_by=first_published_at:desc");
    var request = new RestRequest(Method.GET);
    
    IRestResponse response = client.Execute(request);
    Request
    import Foundation
    
    let request = NSMutableURLRequest(url: NSURL(string: "https://api.storyblok.com/v2/cdn/stories?token=krcV6QGxWORpYLUWt12xKQtt&sort_by=first_published_at:desc")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)
    request.method = "GET"
    
    let session = URLSession.shared
    let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) -> Void in
      if (error != nil) {
        print(error)
      } else {
        let httpResponse = response as? HTTPURLResponse
        print(httpResponse)
      }
    })
    
    dataTask.resume()
    Request
    import requests
    
    url = "https://api.storyblok.com/v2/cdn/stories"
    
    querystring = {"token":"krcV6QGxWORpYLUWt12xKQtt","sort_by":"first_published_at:desc"}
    
    payload = ""
    headers = {}
    
    response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
    
    print(response.text)
    Response
    {
      "stories": [
        {
          "name": "Jimmy Moons",
          "created_at": "2024-02-16T14:35:02.379Z",
          "published_at": "2024-02-16T14:35:04.658Z",
          "id": 445021953,
          "uuid": "51eed33d-855f-415c-ac0d-4404e03b89e1",
          "content": {
            "bio": "Jimmy is a front-end engineer who loves crafting beautiful and fast websites. His favorite technologies include Nuxt, Astro, Tailwind, and Storyblok. As a kid, Jimmy wanted to be the first man on Mars, but he discovered his love for developing. Thus, starting his journey toward his true passion.",
            "_uid": "98811439-d739-4886-a99f-fd2c9ebcf293",
            "component": "author"
          },
          "slug": "jimmy-moons",
          "full_slug": "authors/jimmy-moons",
          "sort_by_date": null,
          "position": -10,
          "tag_list": [],
          "is_startpage": false,
          "parent_id": 445020177,
          "meta_data": null,
          "group_id": "367ab023-f911-4810-b3d3-3fb0d6847ce1",
          "first_published_at": "2024-02-16T14:35:04.658Z",
          "release_id": null,
          "lang": "default",
          "path": null,
          "alternates": [],
          "default_full_slug": null,
          "translated_slugs": null
        },
        {
          "name": "Sarah Saturn",
          "created_at": "2024-02-16T14:34:46.888Z",
          "published_at": "2024-02-16T14:34:49.802Z",
          "id": 445020772,
          "uuid": "e7307a9e-a3c5-47ad-8f6c-a1a36fe528e2",
          "content": {
            "bio": "We like to say Sarah is, \"One with marketing.\" She is a guru when it comes to copy-editing, page ideation, and design. Best of all, she always executes flawlessly. In her free time, she likes to read about space, update Wiki space articles and work on her mini diorama of the Apollo Lunar Module.",
            "_uid": "e83f15e0-27e3-436c-97b9-1ea163804c8d",
            "component": "author"
          },
          "slug": "sarah-saturn",
          "full_slug": "authors/sarah-saturn",
          "sort_by_date": null,
          "position": 0,
          "tag_list": [],
          "is_startpage": false,
          "parent_id": 445020177,
          "meta_data": null,
          "group_id": "caa63bbb-87cc-4f53-bde8-5efb74ef8b97",
          "first_published_at": "2024-02-16T14:34:49.802Z",
          "release_id": null,
          "lang": "default",
          "path": null,
          "alternates": [],
          "default_full_slug": null,
          "translated_slugs": null
        }
      ],
      "cv": 1711108220,
      "rels": [],
      "links": []
    }