Jul 14, 2021 03:02 AM
Hi, I am currently connecting to airtable data via the API. I am able to sort my response in certain fields using desc and asc. However for non text fields in particular single select fields where in the UI you would sort First-Last. What would the parameter be for sorting these fields?
Solved! Go to Solution.
Jul 27, 2021 05:28 AM
The sort order for Single Select fields is not evaluated alphabetically… it is evaluated by the order that your single select options are listed within the field customization options.
You can see this within the Airtable interface itself by trying to sort on a single select field.
If you want them alphabetically, you have to manually alphabetize the options within the Airtable interface by clicking on the “sort alphabetically” button within the field options (or you can manually alphabetize them).
Alternatively, you could create a formula field that equals the single select field, and then sort by that formula field instead.
Jul 14, 2021 04:37 AM
Welcome to the community, @Cameron_Flanagan!
That isn’t possible with the “sort” parameter, but you can specify a “view” parameter instead of a “sort” parameter. In that case, the API will sort your records as they appear in that particular view.
One very important thing to note is that your records can NOT be grouped in your view. Your records must be ungrouped. If they are grouped, Airtable will sort your records in its own random unpredictable order. This is a bug in Airtable.
Jul 14, 2021 05:54 AM
Thank you for the reply scott. The issue with the views is that is doesnt allow nice interaction with the data in custom UI over the top of the API. It seems rather restrictive. So are you saying there is no way to sort data that isnt of type long text or short text via the API?
Jul 14, 2021 08:03 AM
Not sure what you’re asking or what you’re trying to accomplish. You can sort on any field you want, not just long text or single line text fields.
Jul 15, 2021 01:38 AM
Hi Scott, Apologies for the confusion here.
The issue I am having is when making get requests to the airtable API using the sort parameter defined in the documentation.
sort%5B0%5D%5Bfield%5D=STUDY%20NAME
sort%5B0%5D%5Bdirection%5D=desc
This works for me for certain fields. But when I do the same thing for a field that is a single select field the response I get back does not appear to be sorted in anyway. So am I doing something wrong is there another value I should pass for direction for these type of fields?
Jul 15, 2021 04:29 AM
I just tested this on my end, and it works just fine for me. Are you sorting on more than one field in your Get Request, or just the one field? Why don’t you post your entire Get Request here.
Jul 27, 2021 12:23 AM
This is just one field. This is working for other types of field but when I do something like the below where N is a single select field the results do not come back in a sorted order.
[14/07/2021 10:18] Flanagan, Cameron
-H “Authorization: Bearer {{API_KEY}}”
Jul 27, 2021 05:28 AM
The sort order for Single Select fields is not evaluated alphabetically… it is evaluated by the order that your single select options are listed within the field customization options.
You can see this within the Airtable interface itself by trying to sort on a single select field.
If you want them alphabetically, you have to manually alphabetize the options within the Airtable interface by clicking on the “sort alphabetically” button within the field options (or you can manually alphabetize them).
Alternatively, you could create a formula field that equals the single select field, and then sort by that formula field instead.
Jul 27, 2021 05:44 AM
Thanks Scott this has cleared things up. After doing the sort in the UI results are returned as expected.
Jul 27, 2021 11:52 AM
Glad I could help! :slightly_smiling_face: If you don’t mind, could you please mark my comment above as the solution to your question? This will help other people who have a similar question in the future. :slightly_smiling_face: