The code allows for arbitrary sorting and filtering of elements in an array of references in Sanity.io.
Supports Sanity.io version v2.
In the example given an array of references for two types, but you can limit it to one type or extend it as desired.
In the fetchElement() function, you can use a custom query so as to get the required elements.