Sorting by Fields Associated with a Story Type
The sorting of stories by a field defined in the schema of a story type can be accomplished using the sort_by parameter, as demonstrated below.
sort_by=content.headline:asc: Sort in ascending order by theheadlinefield defined for the story type
Example Request and Response
Section titled “Example Request and Response”The following example demonstrates how to sort stories in descending order by their first published date.
curl "https://api.storyblok.com/v2/cdn/stories/?token=krcV6QGxWORpYLUWt12xKQtt&sort_by=content.headline%3Aasc"// Using the Universal JavaScript Client:// https://github.com/storyblok/storyblok-js-clientStoryblok.get('cdn/stories/', { "sort_by": "content.headline:asc"}) .then(response => { console.log(response) }).catch(error => { console.log(error) })$client = new \Storyblok\Client('YOUR_STORYBLOK_SPACE_ACCESS_TOKEN');
$client->getStories([ "sort_by" => "content.headline =>asc"])->getBody();HttpResponse<String> response = Unirest.get("https://api.storyblok.com/v2/cdn/stories/?token=krcV6QGxWORpYLUWt12xKQtt&sort_by=content.headline%3Aasc") .asString();var client = new RestClient("https://api.storyblok.com/v2/cdn/stories/?token=krcV6QGxWORpYLUWt12xKQtt&sort_by=content.headline%3Aasc");var request = new RestRequest(Method.GET);
IRestResponse response = client.Execute(request);import requests
url = "https://api.storyblok.com/v2/cdn/stories/"
querystring = {"token":"krcV6QGxWORpYLUWt12xKQtt","sort_by":"content.headline:asc"}
payload = ""headers = {}
response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
print(response.text)require 'storyblok'client = Storyblok::Client.new(token: 'YOUR_TOKEN')
client.stories({:params => { "sort_by" => "content.headline =>asc"}})import Foundation
let request = NSMutableURLRequest(url: NSURL(string: "https://api.storyblok.com/v2/cdn/stories/?token=krcV6QGxWORpYLUWt12xKQtt&sort_by=content.headline%3Aasc")! as URL, cachePolicy: .useProtocolCachePolicy, timeoutInterval: 10.0)request.method = "GET"
let session = URLSession.sharedlet 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(){ "stories": [ { "name": "Earth's Symphony: Navigating the Wonders and Challenges of Our Blue Oasis", "created_at": "2024-02-16T14:22:09.108Z", "published_at": "2024-03-21T16:48:09.206Z", "id": 444996765, "uuid": "660452d2-1a68-4493-b5b6-2f03b6fa722b", "content": { "_uid": "6bdf037c-f713-415c-a26a-8a9cfc926c85", "image": { "id": 14114772, "alt": "", "name": "", "focus": "", "title": "", "source": "", "filename": "https://a.storyblok.com/f/276232/2560x1946/ee938cf736/earth.jpg", "copyright": "", "fieldtype": "asset", "meta_data": {}, "is_private": "", "is_external_url": false }, "author": "51eed33d-855f-415c-ac0d-4404e03b89e1", "topics": ["solar-system"], "content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "headline": "Earth's Symphony: Navigating the Wonders and Challenges of Our Blue Oasis", "component": "article", "scheduled": "2023-10-01 15:00", "categories": ["45d968b6-5790-4fbb-aa41-5781b8edde51", "d8e48716-0ecf-4059-b568-343bf54e4128"], "highlighted": false }, "slug": "earths-symphony-navigating-wonders-challenges-blue-oasis", "full_slug": "articles/earths-symphony-navigating-wonders-challenges-blue-oasis", "sort_by_date": null, "position": 0, "tag_list": ["Editor's Choice", "Featured"], "is_startpage": false, "parent_id": 444991588, "meta_data": null, "group_id": "8d99d18d-5c97-42cd-a646-51e0c553f26a", "first_published_at": "2024-02-16T14:30:11.990Z", "release_id": null, "lang": "default", "path": null, "alternates": [], "default_full_slug": null, "translated_slugs": null }, { "name": "Mars Unveiled: Exploring the Mysteries and Marvels of the Red Frontier", "created_at": "2024-02-16T14:30:19.768Z", "published_at": "2024-03-22T11:50:20.804Z", "id": 445002665, "uuid": "a2c57c06-2c5c-4819-acb7-0d9c047d16e9", "content": { "_uid": "6bdf037c-f713-415c-a26a-8a9cfc926c85", "image": { "id": 14114865, "alt": "", "name": "", "focus": "", "title": "", "source": "", "filename": "https://a.storyblok.com/f/276232/3500x2000/cd4947ece2/mars.jpg", "copyright": "", "fieldtype": "asset", "meta_data": {}, "is_private": "", "is_external_url": false }, "author": "e7307a9e-a3c5-47ad-8f6c-a1a36fe528e2", "topics": ["space-exploration", "solar-system"], "content": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.", "headline": "Mars Unveiled: Exploring the Mysteries and Marvels of the Red Frontier", "component": "article", "scheduled": "2024-02-29 17:00", "categories": [ "45d968b6-5790-4fbb-aa41-5781b8edde51", "a6af7728-eadf-4428-8cf5-343304857374", "d8e48716-0ecf-4059-b568-343bf54e4128" ], "highlighted": false }, "slug": "mars-unveiled-exploring-mysteries-marvels-red-frontier", "full_slug": "articles/mars-unveiled-exploring-mysteries-marvels-red-frontier", "sort_by_date": null, "position": 0, "tag_list": ["Editor's Choice"], "is_startpage": false, "parent_id": 444991588, "meta_data": null, "group_id": "2e0c427d-63ed-420e-830f-bf1889da0792", "first_published_at": "2024-02-16T14:30:11.990Z", "release_id": null, "lang": "default", "path": null, "alternates": [], "default_full_slug": null, "translated_slugs": null } ], "cv": 1711108220, "rels": [], "links": []}Get in touch with the Storyblok community