Sorting lists by the count of a relationship field

Is this possible? And if not, anyone have a workaround?

You will probably need to use a workaround. Can you give more specifics for your use case?

Sure. I have a collection of restaurants with a relationship property to users to allow users to save the restaurant to their favorites. In the list I want to be able to sort the restaurants by popularity, i.e. the count of how many users have saved the restaurant. But I think I will have to create a separate number field that is updated each time a user saves or unsaves a restaurant?

Yep, that would do the trick!

Hi @pfordmedia could you please share if and how you managed to do this?

You description gave me an idea to update a number field in my collection (as an action when a user clicks on a button) with the value of the number of users who favorited something. For some reason the data in the database does not add up: I see for example 2 users who favorited something, but only a “1” in the specific numbers property.

Appreciate any advice you can give :slight_smile:

Best,
J

Hi and welcome to the forum! When you update the number field you will need to grab the current value of the field and add one to it instead of just inputting the new number.