Forum Academy Marketplace Showcase Pricing Features

Natural Sorting

Hello,

How can I implement natural sorting for a list of things by an attribute?

Take a Book thing as an example that has a title which is a string but containing numbers:

Title:
“6-Book”
“27-Book”

I need this to sort naturally by having “6-Book” first then “27-Book” after.

In JavaScript, this can be solved as described here (sorting - Javascript : natural sort of alphanumerical strings - Stack Overflow) but I don’t have a clue how to do it in Bubble.

Thanks

Someone could overrule me but I don’t think this is doable in bubble. What I would do is create two additional fields, one that contains the text before the hyphen and one containing the text after the hyphen. Then when you sort you have two sort levels.

The book title is a user inserted value and I can’t define upfront any structure for the value, meaning it can be “6 Book”, “L6 Book”, “6_Book” whatever they might add…

Oh, then you might be out of luck. Depending on how important it is to you, you might need to write a custom plugin. Sounds like you know a little bit of javascript.

Ok, yes I’m actually a Software Engineer I’ll look into it then. Do I need to re-write a repeating group like component?

I was thinking about that and I don’t see a way around that. The logic for sorting an RG is built into the RG element and that code will not be open source. I think you’ll have to build your own from scratch.

Can you use JavaScript tool box list shifter or similar to reorder the list? The display the RG off that

Have you checked the :sorted function that you can append at the end of a search to see if there is anything useful there?