Randomize list of objects

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:

  1. 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)
  2. In the routine that retrieves data, get the count of all objects in the table.
  3. Create a random set of numbers within the range of 1 and the number you get in step 2.
  4. Run a query with the following where clause:
    id in (value1, value2, value3,.....valueN)
    where value1, value2, value3,.....valueN are values you calculated in step 3.
  5. 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.


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… :slight_smile:

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:

  1. Switch to the Logic tab and select FUNCTIONS. Click the Marketplace icon:
  2. Select “Array Utils”, select the “Collection Shuffle” and click Install:
  3. The installed function will be available in the COMMUNITY section, under “Array Utils”:
    UI Builder - ConsoleDemo - Backendless 2023-02-17 13-04-24

Hope this helps.


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:
UI Builder - DatabaseCourse - Backendless 2023-02-17 13-06-18

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 :smiley:

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 :slight_smile:

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! :slight_smile:

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!