Save the date! Join us on October 16 for our Product Ops launch event. Register here.
Oct 08, 2019 09:40 AM
I have a table with a multiple linked reference field. In airtable, I can drag and drop the order of these references, and changing the order does have an effect in the return result. However, it isn’t behaving fully as expected.
I’m using graphql, and not sorting by any field which kinda works:
exercises( sort: { order: ASC } ) {
id
name
description
}
My question is if there’s some field tied to the drag and drop reordering of references within airtable, or else how else I should handle ordering of multiple linked references. I’m not sure how to solve the problem otherwise, since there could be one or many references, and they’re all shared resources so can’t have an “order” field (an individual could be referenced many times in the parent table in many possible orders).
Oct 08, 2019 11:13 AM
Welcome to the forum Zac!
I have often wondered this myself with regard to attachments because there’s no mention of a natural field order indicator in the API documentation (that I’m aware of).
I’ve also used GraphQL with good results.
Oct 08, 2019 04:42 PM
Thanks! The interesting thing I’ve noticed is that when I change exercises( sort: { order: ASC } )
to exercises( sort: { order: DESC } )
, the order does change.
Original order in airtable:
A
B
C
D
E
Order with sort: { order: ASC }
:
A
B
E
C
D
Order with sort: { order: DESC }
:
D
C
E
B
A
You can see that it’s almost respecting the ordering, but the last element E
is appearing in the middle of the pack. I tried removing them all and adding new records in the right order, that also didn’t behave as expected. Maybe a bug?
Oct 08, 2019 05:09 PM
When adding them a second time did they resume the same order as before when they sorted ASC and DESC with repeatable, if not unexpected, orderings?
If they did, then I would be inclined to say that there’s some attribute, or a part of an attribute in the array, that’s used to determine sort order. I wonder if attachment collections behave this way as well.
I would love to hear @EvanHahn’s take on this. If we understood the sort mechanism for we would at least have a chance to engineer the content to fit the behavior.
Oct 08, 2019 08:37 PM
Ahhh, your reply got me thinking since the behavior has been random. I ran a test and think it’s automatically sorting by id alphabetically. I created 5 brand new records to and spit out the entire object. While the order was yet again different, the IDs are shown alphabetically.
`
// ASC
{ “Id”: “Rec2yzMMT4k6tazdL”, “Name”: “B”, “Description”: “” }
{ “Id”: “Rec8loxSYHEf9Bel6”, “Name”: “D”, “Description”: “” }
{ “Id”: “Recq3S0bCbfdsmVoP”, “Name”: “C”, “Description”: “” }
{ “Id”: “RecwwoGcKacsAMYTP”, “Name”: “A”, “Description”: “” }
{ “Id”: “ReczZ5fz6DM9ZclI8”, “Name”: “E”, “Description”: “” }
// DESC
{ “Id”: “ReczZ5fz6DM9ZclI8”, “Name”: “E”, “Description”: “” }
{ “Id”: “RecwwoGcKacsAMYTP”, “Name”: “A”, “Description”: “” }
{ “Id”: “Recq3S0bCbfdsmVoP”, “Name”: “C”, “Description”: “” }
{ “Id”: “Rec8loxSYHEf9Bel6”, “Name”: “D”, “Description”: “” }
{ “Id”: “Rec2yzMMT4k6tazdL”, “Name”: “B”, “Description”: “” }
`
So I guess the question becomes — is there a way to access whatever data is available that airtable itself uses to persist record order in the UI?
Oct 08, 2019 08:52 PM
Okay, I think I figured it out. Posting here for posterity — not sure if this will solve your problem or not, @Bill.French.
The default ordering of the records in the array that is returned on a linked record field correctly matches what is in the airtable UI, as long as you don’t pass any graphql sorting arguments. Because my initial goal was to reverse the order from airtable, I tried changing the sort method to DESC, which implicitly sorted on the id field. By removing the sorting entirely, it works as expected:
exercises {
id
name
path
}
Now I just have to sort manually within airtable, which is a pain but not the end of the world.
Oct 09, 2019 07:34 AM
Ha! That’s what I guessed. So, does this mean that if you use the API to sort a result set, it causes Airtable to permanently remove the manual sort order from that moment forward? Or, is the natural order sustained despite sorted API requests?
Possible Solution?
Design your queries to always use the natural (UI-based) order. Take the result set and make a snapshot array while adding an index key attribute. Then sort on the new key descending. That would give you the exact opposite of the UI-based order.