Sort by Calculated Value

Thank you Robert. #2 looks intriguing. How would you implement it? Here’s how I attempted it:
(1) Make the sort button change the state of the RG before proceeding to sort the RG
(2) Add a condition to the Expression element such that When the state of the RG is a certain input, the condition evaluates an expression that tries to assign the value of the Expression element in the RG to the sort field.

Problem is in (2), the expression in the condition reads like “sort field value” is “Expression value”. I used ‘is’ because ‘=’ is not available. As such, the value assignment didn’t really work.

How would you implement it?

It’s an old project, but I remember using the plugin to extract the calculated values. Don’t think I sorted them, but it seems if it is possible to extract the calculated values from the RG, sorting should also be possible…perhaps with a step in between.

So I made a test…shopping cart style…to see if this could work. The key is to create a “line item” data type that holds the item, quantity, and total in the same record. Then you can sort by the total.

There are 3 datatypes:

  1. Widget (item) - fields: Name (text) and Price (number)
  2. Line Item - fields: Widget (widget), Quantity (number), Total (number), and Purchase Order (Purchase Order)
  3. Purchase Order - fields: Line Item (list of line items), PO Number (number)

The workflow must calculate the total in the database so it can be sorted.

here is a link to the editor, so you can see how it works:

And it does work. The repeating group updates automatically based on the total amount (sorted by this in the do a search for)

Thank you Robert. Really appreciate your work on this. The calculation occurs when the RG is loaded, so real time somewhat. That’s why Expressions are optimal. I agree - sorting with the extracted calculated values require an intermediate step. I think that step involves storing the extracted values (list) to the database - a field of the data in the RG, for the sort function. Would appreciate any idea on doing the storing

I tried “Make Changes to Things”. I was able to reference the extracted list but could not make index number variable to update the data points of the RG field with the corresponding member of the extracted list

Without knowing your particular setup, I can’t offer specific advice. Perhaps you could include a link to your Editor.

Generally speaking, I think the key is to have another Datatype that is linked to your “things” so you can sort by a calculated value that is stored in the database (like I did with the “Line Item” datatype). This datatype acts as the variable based on the user inputs.

Rather than referencing the extracted list, I think you need to reference this special Datatype (i.e. line item) that is specific to the order or user, depending on your setup, and make the calculation based on some action taken by the user.

Thank you Robert, I will share an example in bubble editor in the coming days to make the situation clear. Really appreciate it!

Here’s a link to the example.

User enters a color. Colors are displayed in the repeating group. As User increases or reduces votes, Expression calculates trend factor in real time (because there is a time component; trend will be stale if it is saved to the database at the time the user votes). The goal is to sort the repeating group by the trend factor. How do I implement this?

@chrismchewols You could have it run a workflow on page load that recalculates it to the database. I’ve done this in the example. Check it out!

Note: I also had to change it from a text field to an input field so it can read/save to the database properly. Unfortunately, the input field will not display out to four digits properly. (this may be a bug). So I brought back the text field as a display only, and hid the input field.

Ah, thank you Robert. I like the set up. Issue is, on page load, “trend factor = This Color’s trend factor” is not updating the database with the output of the Expression. I added a Sort Button with the same instruction for good measure. I also displayed the trend factor in the database and the trend factor output of Expression to be sure.

1 Like

This seems like a good option, but doesn’t support a to-many relationship.
If a Contact stores a list of Transactions, and you need to display the Contacts ordered by the number of Transactions, this is surprisingly hard to pull off in Bubble.

This is why I added a SORT action to List Shifter. You can do any type of mapped sort. (List Shifter can of course also help you do things like build lists of values to sort by.)

3 Likes

Ahhh thanks! I heard about List Shifter previously but had forgotten about it :slight_smile:

Hi @mikepost80, can you tell me if List Shifter helped you in this particular case ?
I have exactly the same problem :slight_smile:

I installed List Shifter and Repeating Group and I can’t see my calculated value…

Hi Keith, do you by chance have an example of how use your SORT (list shifter) to sort by a calculated field? thank you

3 Likes

Hey Keith, can you explain this further about sorting by a calculated value? Or if you have a link to an example?

My particular example, I need to sort by Thing’s Date + days [number field of days] - Current date/time:formatted as days and sort by the resulting number.

This guy has a great short video on how to use List Shifter’s SORT action to sort on something you couldn’t usually sort on in vanilla Bubble:

Once you understand that, all you have to do @tylerboodman is generate that list of numbers and use that in the SORT action.

@tylerboodman further to my previous response, you’ll be interested in my NEXT video doc/demo about Floppy as I’ll be showing the List Math action, which makes computations on a list of numbers a snap.

I had meant to point to the video I did about sorting by calculated values / sorting by the count (number of items in) of some Thing’s list-type field, but had forgotten to do so. Here it is:

Originally shared here in the Floppy thread.

1 Like