I want to return a dataset in random order (a fixed number of objects, but sorted randomly) every time. What would be the easiest/preferred way to go about randomizing a list in a Backendless/Codeless API method?
The “simplest” way I can think of would be comprised of the following:
- Create a new “auto increment” column in your table schema. This will assign a numeric id to every object (presumably starting with 1). Say the name of the column is
id
(it will be used in step 4) - In the routine that retrieves data, get the count of all objects in the table.
- Create a random set of numbers within the range of 1 and the number you get in step 2.
- Run a query with the following where clause:
whereid in (value1, value2, value3,.....valueN)
value1, value2, value3,.....valueN
are values you calculated in step 3. - The resulting set may contain fewer objects than expected since some of the objects may be deleted and thus some
valueX
values will not exist.
If you can rework the algorithm to skip the count API call, it would be desirable as counts are quite expensive from the performance perspective.
Regards,
Mark
The number of items/objects is fixed luckily, so that should be no sweat.
I was trying to do it like this (Screenshot), but that did not work (Error: Cannot convert undefined or null to object), so I’ll see if I get better luck with your proposal…
So rather than getting a random set of objects, you need to shuffle a collection of data. Is that right?
Correct - the whole story I want to achieve here is as such:
- Fetch a fixed number of objects
- Scramble this collection
- Present these to the user in chunks (e.g. 5 at a time), so the user can score/answer
- When all presented data has been scored/answered, save the resulting dataset, or maybe save each time the user is ready with a chunk
I just published a UI function to our marketplace that will do exactly what you need. To get the function:
- Switch to the Logic tab and select
FUNCTIONS
. Click the Marketplace icon:
- Select “Array Utils”, select the “Collection Shuffle” and click Install:
- The installed function will be available in the
COMMUNITY
section, under “Array Utils”:
Hope this helps.
Mark
I just noticed you’re using it in Cloud Code and UI Functions are not available there. Here’s how you can duplicate the functionality:
This is my custom function:
This is what’s inside the Custom Code
block:
The same code as text:
return list.map(value => ({ value, sort: Math.random() }))
.sort((a, b) => a.sort - b.sort)
.map(({ value }) => value)
Wow, thanks! That is beyond service
In related news, I actually figured out why my code was failing (doing babysteps here on my side), and I figured it out, so I also solved it “my way”:
Guessing it is similar to how you solved it with that function
Regardless - thanks a mill!
Oh sweet - so you can mix “proper code” with the codeless elements when needed? Did not know, thought it was one or the other… Great, then I know I have a fallback for more hard problems!
You’re welcome.
Btw, in your codeless logic, I recommend adding elements into RandomList
at the end. I think “insert at…first” would run slower.
Thanks, makes sense!