Is there a way to sort a reference column?

Hey guys, quick question. I want to be able to filter a dataset by a reference column. Is there any way to do this? And if not, is there any way to auto-copy the reference into another column that I can filter? Also, is there a way to sort a dataset by date edited?

Thanks in advance

1 Like

Hey Phil,

Currently, there isn’t a simple way to sort by Reference field, but you can code a workaround by adding field value from referenced collection into separate column that will be filterable. Your code might look something like this:

import wixData from 'wix-data'

export function CollectionWithReference_beforeUpdate(item, context) {
    // Get id regardless of whether you're adding the item through site or Content Manager 
    const referencedItemId = item.referenceField._id || item.referenceField
    
    // Get data from referenced collection
    return wixData.get('RefCollection', referencedItemId)
        .then(ref => {
            // Add value that you'll use to filter your collection
            item.filterableField = ref.fieldToUseForFilter
            return item
        })
}

And lastly, you can easily sort Dataset by field Last Updated.

Hope that helps!