Ordering articles by views in deskStructure.js for Sanity.io
Your code is almost correct! The method you're looking for is defaultOrdering() instead of ordering(). Here's the fix:
.child(
S.documentTypeList('article')
.title('Artikler efter sidevisninger')
.filter('_type == "article"')
.defaultOrdering([
{field: 'views', direction: 'desc'}
])
)The key differences from your code:
- Use
defaultOrdering()instead ofordering() - Pass the ordering array directly as an argument (not wrapped in an object with
byandtitleproperties) - The array contains objects with
fieldanddirectionproperties
So the correct pattern is:
.defaultOrdering([
{field: 'fieldName', direction: 'desc'} // or 'asc'
])You can also specify multiple ordering criteria if needed:
.defaultOrdering([
{field: 'views', direction: 'desc'},
{field: 'title', direction: 'asc'}
])This will order your articles by views in descending order (highest views first) in your desk structure. The defaultOrdering() method is part of the Structure Builder API and works with both S.documentTypeList() and S.documentList().
One gotcha to be aware of: If you select a custom sort order manually in the Studio UI, it will override your defaultOrdering configuration and save that preference to local storage. If your configuration doesn't seem to be working, try clearing your browser's local storage or testing in a different browser.
Sanity β Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.