Sorting a RG based on :count list of dates

I’m saving dates of an event into a list of dates within a user group.

I need a list of all users, sorted by the total # of items in that date list.

When I get data source from the sorted Date:count “Fecha Terminada”, it seems to break the RG.

Not sure what I’m doing wrong…

1 Like

That’s not a valid value for “dynamic field name”. Is the stupid Issue Checker not warning you about that?

It’s nice and blue, making me think I nailed it, when in fact, nothing is showing…

1 Like

I’m pretty sure you just mean “Fecha Tetminado” there.

Sorry, my autocorrect only speaks English… but you get the idea. :stuck_out_tongue_winking_eye:

Thanks for the guidance Keith. Fecha Terminado (date complted) isn’t an option under there. Writing it out doesn’t work… would love your guidance here…

The feature you’re using here (n your screenshot) is not “sort by”, it’s “change the field we should look at to sort by”. Consult the reference for details on how that works, but I don’t think that is what you are trying to do. (I’m mobile at the moment.)

The Sort field in the Search configuration dialog will only let you select a field that exists on the Thing you are searching.

You can also add :sorted by after the fact. But again, you can’t take some arbitrary list and and use that as a sort criteria.

But it sounds like you’re trying to do a different “mapped sort”?

A mapped sort is like this:

Let’s say we have a list of Users which I will represent below by first name. The list is:

Amy, Pedro, Petra, Becky, Thomas, Keith

And now I have another list that corresponds to some other quality. Like, let’s say that (I think this is what you are trying to do) Amy is going to 5 concerts in the future, Pedro is going to 6, Petra is going to 1, etc.

And that list is:

5, 6, 1, 2, 30, 3

Put in tabular form:

User # of concerts
Amy 5
Pedro 6
Petra 1
Becky 2
Thomas 30 (Thomas really likes concerts!)
Keith 3

And now we want to sort the number of concerts (Descending) and have each User follow along. So that we would get:

Thomas
Pedro
Amy
Keith
Becky
Petra

Well, Bubble cannot do that… Unless # of concerts is a field that’s already on the User object.

Then you’d just sort by User’s “# of concerts” field.

Of course, this is a little bit annoying.

The SORT List feature in my List Shifter plugin lets you do this, however. If you load the list of Users into List Shifter, you can then run List Shifter’s SORT List action on it and supply the # of concerts list as the list to sort on.

Is this something like what you are trying to do?

4 Likes

(Also, I think that you don’t need List Shifter to do this, but you do would need to use the :sort by operator if it’s at all possible … I’m not in front of a real machine right now to compose that wacky expression, but will be in a short bit…)

Classic Thomas, never misses a concert!

It’s kind of like that, only everything is packed in the User object.

Dates completed is a list of dates in the User object. I’m trying to sort based on the total items in that field filtered by some constraint.

I want this RG to be filtered, in order of total date items in that label.

I want this RG to be ranked based on only dates from the past week, for example.

Hope this explanation makes sense…

2 Likes

Hey @despega: yeah, I get what you’re trying to do. (Glad that my example is not too far off what you are trying to do!) In my example, the issue is this:

Let’s say that I have a list of “Concert Dates” on each User. For example, Thomas is going to 30 concerts and so his list of concert dates is 30 dates long.

So, if I have a handle on User Thomas, I can say:

UserThomas’s Concert Dates :count

And that expression will yield 30. Similarly:

UserAmy’s Concert Dates :count

… will return 5 (Amy is going to 5 concerts in the future).

Now it sounds like we’re all good, right? Because knowing how to get one single user’s count of concert dates, can we get the count of concert dates for a LIST of Users?

Well… NO, it turns out. Here’s why: Let’s say we have a Search that will result in a list of Users that is as follows (let’s call this UserList for the sake of the below explanation):

UserList” is: Amy, Pedro, Petra, Becky, Thomas, Keith

OK! So now I do:

UserList's Concert Dates :count

:point_up: what does this return? We hope that this is a list of Concert Dates (which is itself a list of dates) and that, then, each list of Concert Dates is turned into a :count. (So that we have a list of numbers representing the number of concerts that each User is going to.)

BUT, that’s not what this expression returns. :cry:

Here’s what happens: Bubble expressions are evaluated from left to right. UserList resolves to a list of Users:

Amy, Pedro, Petra, Becky, Thomas, Keith

So far so good. Now, the expression says:

Amy, Pedro, Petra, Becky, Thomas, Keith's Concert Dates

:point_up_2: We hope that this is a LIST of lists of Concert Dates… BUT it is not… It is a list of dates like this:

Amy's Concert Dates concatenated with Pedro's Concert Dates concatenated with Petra's Concert Dates concatenated with Becky's Concert Dates concatenated with Thomas's (massive list of) Concert Dates concatenated with Keith's Concert Dates

That’s going to be a list of 5+6+1+2+30+3 Concert Dates (47 Concert Dates total). So now we just have a list of 47 Dates.

We can never construct a list of lists in Bubble really. We always just get a vector (a 1-dimensional array).

Foo.

So now we have some LIST OF DATES and now we take its :count:

LIST OF DATES :count

And we will get the length of that list (sort of). If every date in that list is unique, 47 will be returned to us. HOWEVER…

… when we try to take the :count of that list, the list will be “Bubbleized” and duplicates will be removed. (When we do Bubble operators on things that are arrays, they are first turned into Sets – arrays of UNIQUE items.)

So, let’s say that all of the dates in that list are unique EXCEPT for July 4, 2020 (Amy is going to see the “Beefy Weiners” on July 4th and Thomas is going to see the “Gizmotic Modulators” on July 4th – both are great bands, how could you choose?) and ALSO, April 20, 2020 (it turns out that Petra is going with me to see the “Canned Cat Quartet”… unless she flakes out again).

So, in that case, :count would return 45.

In any case, none of this is helpful because we didn’t want a scalar value.

5 Likes

So what can we do?

Well, IF whenever we push a new Concert Date onto a User’s list of Concert Dates, we also set some scalar field on the User to that list’s count (let’s call that User’s Number of Concerts), we can sort by Number of Concerts.

But what if Number of Concerts does not exist? Well, we’d need some other way to do it.

Enter List Shifter…

(And now, I guess I have to actually build this example to show you how we would do that…)

3 Likes

Your explanation reads like an adventure novel. Just wanted to keep reading to find out what would happen!

(ps. I went to see the Canned Cat Quartet, it’s overrated. So if Petra flakes, it’s not a big loss!)

Thank you for taking the time to write that educational piece of entertainment. You should do this more! Tutorial website? I’d pay for that.

Just installed List Shifter… excited to test it out!

1 Like

But wait… there’s more, @despega! (Might take me a minute as I’ll be eating dinner. Also, Petra keeps texting me and my wife hates when that happens…)

But here’s where we are so far: I’ve created a proxy for Users called “Concert Goers” and it looks like this:

And next I’ll be assigning a list of Concert Dates to them…

(I’m going to build a page using Calendar Grid Pro to easily pick multiple dates for those concerts to make it easy…)

3 Likes

I’m loving this. Can we publish this thriller / mystery novel after this is solved?

3 Likes

OK, so now I have recreated the EXACT “hypothetical” situation I proposed above:

You will note that each Concert Goer has exactly the number of concerts I described previously.

Also (TOTALLY HYPOTHETICALLY, mind you) Petra and I are going to a concert on the same day (SHHHHH… don’t tell) and also, Amy and Thomas are going to concerts on July 4 2020 (but, even though it’s not reflected in the data, those are totally different concerts).

I did this by creating the little admin interface you see in the image (that calendar is Calendar Grid Pro and it makes it easy to do stuff like this – I just selected each Concert Goer, clicked dates for their future concerts. Also, sometimes I set CG Pro’s “blocked dates” so that I didn’t accidentally select a date that is SUPPOSED to be unique.)

But now you see I have this list of “Users” with concert date list lengths just as I described before.
… to be continued…

4 Likes

So, now that we have our test scenario, let’s see if there’s a way to sort this the way you need: First. we create a copy of the RG to visualize a new sort order (at this point, the RGs are identical):

And now we can experiment. Was I correct about what fields we can sort on?

:point_up_2: NO… we can only sort on fields that are in Concert Goer (our proxy for “User”)… Since we don’t have a “Concert Count” field, we can’t sort on that.

Hmm.

2 Likes

OK, so what about the list of Concert Dates? Is that truly just a vector?

Let’s check:

:point_up_2: Here are the expressions we are checking… And here is how it looks in run mode:
Looks like I was wrong about collapsing duplicates on :count… Full transparency:

There are still duplicate values in our list…

… however, the point remains the same: :count on this expression does not yield a LIST OF COUNTS, but just a scalar value… 47.

And, more importantly, you see that what we thought might be an array of arrays (concert dates) is just an array (a vector/list of concert dates)… We have no way of figuring out how many concerts each User is going to (except… you can see them in the RG’s right? We’ll get to that…)

CONTINUING:

You might have noticed that I already put our SEARCH into a List Shifter. (List Shifter is, amongst other things, a way to store a list and then reference it easily.)

And you might have noticed something else: You say, “Keith, I can see the ‘number of future concerts’ in each line in those repeating groups? Why can’t we just snag them out of there?”

To that, I tell you this: Repeating Groups are just a form of the “for each” loop… For each item in the repeating group, we can compute certain values… but those values are not held in any variable that we can access.

So, there are certain plugins that let you snag those values out of the page… literally grabbing them out of the repeating group and bringing them back into Bubble. But this is dumb… BECAUSE WHAT IF I DON’T WANT TO SHOW THE REPEATING GROUP, eh?

Hold on… Petra is texting and is wondering why I called her “hypothetical” in a previous message…

AND CONTINUING…

How can we get a list of “Concert Date counts”?

Here’s how my List Shifter is set up: It holds a list of “Concert Goers”:

But now, in its PROCESS Output type, I’m going to select “number”:

And now, I’m going to program it to transform the list of “Concert Goers” into a list of “number of concerts” each Concert Goer is going to. We will use the PROCESS List action to do this. But…

One of the strange and wonderful things about List Shifter is that it does not know the values of the items that it holds. Further, if it holds Things, it does not know the values of the fields of those things. But now we need to tell it, “Hey, I care about the ‘Concert Dates’ field because I want to inspect that in the PROCESS List action.”

So we do this:

First, set up a workflow triggered by “when this List Shifter is Initialized/Updated”:

And then we get:

And now we can build its workflow. We will use PROCESS List (and we can put one of those in the workflow), but first we will add an action called “SET Process Field of Interest”. We do this so that List Shifter can access that field:

:point_up_2: as our field of interest, we select “Concert Dates”. (Remember that “Concert Dates” is a list of dates.)

And now we add PROCESS List… and I’m setting the list to process to be the List Shifter’s Original List:

What PROCESS List will do is, FOR EACH item in the original list, do some functions… Essentially duplicating what a Repeating Group does.

When we continue, we’ll program those steps. …

5 Likes

This is beyond awesome! I’m giving this thread several reads over to fully absorb its wisdom.

P.s. LOL, I’ve never called a girl “hypothetical” but I can see how that would sting a bit. :rofl:

Eager for the steps to come!

Now that @despega replied more, I can reply more (seem to have hit a spam-filter… you can only reply so many times in a thread, apparently). Our next step is to use SET Process FIELD of Interest for our List Shifter. We need to get at the values for “Concert Dates”. By setting this as our “Field of Interest” we can access those values in the PROCESS List action:

Now for our PROCESS List step: What we will do is take each item in the “Original List” (this is the list of Concert Goers). For each Concert Goer, we get the “Field of Interest” (which is a list of Concert Dates) and use the “Array x.length” operator on it. (This is the same thing as Bubble’s :count operator.)

And then we will push that value on to List Shifter’s Processed List:

And in design mode, I’ll just display that list in text element, so we can check our work:

Let’s go to preview and see if it works!:

AWESOME! Now we have our list of number of concerts each Concert Goer is going to, just like we need it.

Now we can use List Shifter’s SORT action to do the mapped sort:

:point_up_2: To do the sort, we need to provide some information. We tell List Shifter about the “Sort by” list – this is the list of values that we want to use to sort the Original List. In this case, we are using the list of numbers held in List Shifter’s Processed List.

The SORT action has three different sort modes (for Number, Text and Date). In this case, we want to use Number. And then of course we can tell it to sort either ascending (the default) or descending.

Now what will happen is that list which represents “# of concerts” will be sorted low to high… and our list of Concert Goers follows along with it. We cannot actually change the order of our Concert Goers in the Original List, so our sorted values appear at List Shifter’s “Shifted List” output.

To check my work, I again use a text element to see what I’ve done:

Now in preview mode, let’s see what we’ve got!:

BINGO! Now we can make that the source for our “SORTED” repeating group and this page will make sense again. So I set the RG on the right to be sourced from List Shifter’s Shifted List:

And I’ve hidden my “debug” text element and our preview looks like this:

Now, you might be thinking… GEEZ, this seems kinda complicated and does it take a long time to compute? The answer is no, it all happens in the blink of an eye. Check out this demo page here:

https://timezoner.bubbleapps.io/version-test/concert-goers?debug_mode=true

(Editor here.)

9 Likes

This topic was automatically closed after 70 days. New replies are no longer allowed.