Why is sorting a RG dynamically so difficult?

I’m probably making it too difficult :wink:

I’m trying to sort a RG dynamically which works actually very well, as long as the things (fields) you sort on are on the same level as the source of the RG. I have headers for each RG column that set the RG’s custom state of ‘sort’ to the field name’s value I’d like to sort on. Like so:

RG source = Search for ‘Jobs’
Things (fields): name, Created Date.

Click on header text ‘JOB’ or header ‘CREATED DATE’:

Custom sort RG: ‘name’, ‘Created Date’

This works great until a second level Thing comes into play.

Sorting on Job’s name is no problem, but sorting on Job’s Account’s name seems impossible using this method as Bubble does not seem to figure out which field to select for the sort. I’ve gone through many of the related topics on this forum, but couldnt find this specific issue.

I can add new conditions that if ‘this RG’s custom sort = value x’= data source= something to make sure to filter on a Thing’s thing but: 1. this is not very straightforward and 2. most often just doesn’t seem to work.

So how are you sorting RG’s on multiple fields? And why can’t I just select the second level thing to sort on and by default it’s primary field will be used?

1 Like

I agree that this is pretty tricky. You’re not the first to bring it up. I tend to make it easier on myself by just adding a new field to the first level Thing so that I can sort by it instead. If it’s mission critical, then it’ll be worth the management of an additional field.

2 Likes

Thanks. Adding new fields is not something very viable, it will mean a significant change in the data structure and a lot of additional workflows.

I really like the dynamic sort in combination with a custom state. Very clean and elegant. If only that would allow for selecting a secondary thing.

@emmanuel, is this something that is possible? Could it be possible to select the primary field (or any field, if I may be greedy :D) of the secondary thing you are referencing?

In the actual RG, you can select any connected thing as many levels deep, so it should be possible with sorting to right?

2 Likes

It is hard because it is inefficient to do it from the “outside in” (so Job > Account) … so get a million rows back from the database, including linking to another table, then sort that million rows (in “memory”) on the returned data and then pick the top 20 and chuck away 99980 rows. You would face the same issue with an SQL query that accessed the data this way, it would end up scanning the whole table first. You would write your query in a different way to get round this.

Far more efficient to get the first 20 from the “inside out” (so Account > Job) and then list the Job data from that 20.

(assuming that you are only displaying the first 20 in your repeating group)

It is a little confusing, however. You can only sort on the Search for fields on the search’s data type. And the reference says that if the dynamic field is not found then it won’t sort. So it looks like it will work to put “Account Name” in there, as it won’t give an error, but it just won’t sort.

The way to do it, as @romanmg says is to have a list of Jobs on the Account, and then go from Account > Job and sort on the Account.

2 Likes

If I understand correctly, you can in fact sort on a secondary thing when you do not state the data source as:

do a Search for Job

but instead do

do a Search for Account’s Job.

This allows you to sort dynamically on Account and also lets you add a :sorted or :filtered constraint on the Job part of the string allowing you to sort on that.

However, my problem is with the fact that I have one central data type ‘Job’ which has a lot of other data types connected. In my case, a Job can have multiple Users, Accounts connected. But also a data type called ‘Contact’, ‘Calculation’ and ‘Invoice’ as well as ‘Files’.

In the example I gave initially it is not just one secondary thing I need to reference, but multiple. I do not seem to come any further than two at this point with the solution you stated and my example above because even when you merge a data source, that second data source should be of the same data type.

This is why I come back to my initial proposal as for me it is far more logical to reference (within constraints) all the other Things through the one data type that connects them all (the Job). Is this from a data architecture perspective illogical and inefficient?

In the RG I can easily list ‘Current cell’s Job’s Account’s Contact’s email’ (going three levels deep), but sorting on that email field will be impossible?

If Job can have many Accounts, then that is going to be difficult to sort on the Name as there will be many ?

Yes, but that is a different situation. Listing the data is one thing, using that data in combination with all the other accessed data to sort the whole thing is another.

My actual problem is not with the Accounts type, but it is an example of a data type that I’d also like to include in the RG and be able to sort on. In this case, there are constraints on the Account based on, for example, a status or a type field which results in just one Account to show up.

As I understand sorting a list alphabetically on whichever field that is in the RG is far more complex that I initially assumed, is it fair to say it is impossible to sort a RG this way? In that case I would probably have to look into another solution, something more along the lines of a search result in an entirely new RG.

It isn’t impossible, you could do your sorts using a different “data source” based on a conditional rather than dynamic sort fields. Although this is a little more complex.

Having the data relationships on both “ends” of the two things that are related will make your access easier though.

sorry, I meant sorting dynamically this way would be impossible.

I had the conditions first, but it becomes way too complex to manage because of the amount of sort options as well as different user roles to account for.

Is there a reason the primary field of the secondary thing cannot be referenced during dynamic sort?

I would imagine it is down to performance but it does make it harder not being able to do so, that is true.

Sorry can’t be of more help, not sure there is another way round it other than by changing the data.

No problem. I might have to look for an alternative solution more search based with search results in different repeating groups instead of sorting the list.

Just wondering if @emmanuel has an idea for dealing with the secondary thing’s field as a dynamic sort value…

Did you ever find a solution here? I’ve run into the same issue and can’t find a way around.
My Post: Toggle Sorting in Repeating Group Search Results

1 Like

Bubble seems to be really lacking in both sorting and generally accessing data through RGs. Right now I’m trying to display data from Thing1 sorted by Thing1’sThing2’sField.

I can display the fields I want by using data source “Search for Thing1” but then I can’t sort the way I want.

I can sort as desired by using “Search for Thing1’sThing2:sorted” but then the only data I can actually display is from Thing2, which isn’t what I want.

aargh.

image

A list of Books, sorted by the Author’s Name.

So Author has a List of Books.

Book has an Author field. Author has a list of Books field.

More complicated ? …

image

List of book’s sorted by the name of the author’s dog.

1 Like

I’m following the logic in the responses, but still I am unable to get passed an issue with sorting. I have watched all available tutorials, have tried nearly a dozen different logical sequences to perform the sort with no luck.

Basically I want to sort my search results in a repeating group by two different data fields and see the results show all matches for both data fields.

I have Property Listings as a data type. I have Property Types as a data type. I have property types (list) as a data field for the Property Listings data type.

I can sort fine when selecting only one property type. However, when I select a second property type I get no results displayed. I want users who select houses, condos and hotels to see the results display all the houses, condos and hotels that meet the criteria of the other search parameters.

Anybody have a way to do this?

For these specific issues it’s best to recreate in an app or provide access to your app so others can have a look.

aha! Thanks @NigelG! If I’m understanding this correctly you are showing that Bubble requires a different way of thinking about sorting… rather than searching for the thing we want to display then traversing the graph to get the data to sort by, we need to search for what we want to sort by, then traverse the graph to get the data we want to display.

That has data design implications showing why it is important to link in both directions, and it would seem to have performance implications as well… eg, a list of customers sorted by their most recent order’s date would have to be:

bub2

But wouldn’t that search return every order in the database and make Bubble’s server sort them, then go back to the database again for each order to get it’s associated customer? If I’m understanding correctly it would be more efficient to put the sort in the search then Bubble would retrieve only the first batch of items:

@romanmg fyi

Below is a link to my app editor. I have it set to “everyone can edit”.

I’m trying to get the sort to function on multiple levels just like the big OTAs like booking.com or Expedia have set up. So a user could select multiple property types, multiple property facilities, multiple neighborhoods, a base review score and then sort all those results by either price, review score etc.

If anybody has any ideas on how to make this work it would be a life saver…literally.

I’ve created a new forum post. Provided link to the app that is viewable to get as much detail as possible.

Cheers

the reference says that if the ListFild shouldn’t contain more than 100 value. What if there are much more than 100 values?