sanity-io / sanity

Sanity Studio – Rapidly configure content workspaces powered by structured content
https://www.sanity.io
MIT License
5.27k stars 426 forks source link

Add sorting option to reference fields #3143

Open bjornwang opened 2 years ago

bjornwang commented 2 years ago

Is your feature request related to a problem? Please describe. Sometimes reference fields are used for small, well-defined sets of configuration values. In these scenarios it would be great to be able to sort them.

For example, in this case (see screenshot), the document type has a length field used for sorting which would be great to use for the reference selector too to get the shortest period at the top of the list. (The list contains predefined identifiers used in integration with a legacy system, hence the strange selection of durations.)

image

Describe the solution you'd like Add a sort order option just like we have optional filters for references today.

Describe alternatives you've considered Prefixing the values with numbers or alphabetic characters to affect the order.

jonathanquaade commented 2 years ago

Any update on this? This feature would be life-changing

miked-known commented 1 year ago

Agreed, this is a sorely needed feature particularly for reference arrays with mixed types... Any ETA on this?

sebbean commented 10 months ago

i, personally, would really enjoy this feature immensely

qruz-hq commented 8 months ago

Ok this feature actually exists, I guess it's just not that well documented:

defineField({
      name: 'articles',
      type: 'array',
      of: [{
        type: 'reference', to: [{ type: 'newsArticle' }], 
        options: { 
          sort: [{ field: '_createdAt', direction: 'desc' }] // SORTING REFERENCE FIELDS SEARCH
        }
      }],
      title: 'Articles',
      hidden: false,
})

Full credit: https://github.com/sanity-io/sanity/issues/2295#issuecomment-1743936873

ErlendKH commented 1 month ago

Can anyone confirm that it still works? No sorting for me - I'm using Sanity 3.54.0. By the way, I use defineArrayMember inside the "of".

tomahg commented 1 week ago

Still works! Using Sanity 3.61.0.