Sort random - Datatable

Hello Everyone,

Is there a way to sort the datatable in a random order? I mean I would like to shuffle the rows of my datatable. Can anyone please help me? I don’t want it follow any order like ascending/descending. I want to induce randomness to the data order. Please note am using version 2017.1.6682. There is no filter datatable activity as such. I usually use select clause for that but how do we do it when we want the data to be in random order?

I’m not really sure how you would do this or what youre exactly asking for ?

Do you want to just “shuffle” the current datasheet ? if thats the case i wouldnt know really…

If you want to pick random rows, then you could assign a random number to a variable :

Assign : [randomNumber] = [new Random().Next(1,10) ] ( if there are 10 rows)

Then you can use the “for each row”

and if row index = random number > Then

See attached robot.

RandomRowSelector.xaml (13.0 KB)

Edit : Thinking on this more, if you did want to make it a “shuffle”, you could store each random number in a list then filter that out. That way you would never get the same row twice. Im not sure how you would tell the bot when its “done” tho, as it would still select the random numbers, but it would just not do anything with them…

So when a number is selected = add that number to list
then do a for each item in list = if list = random number = do nothing.

so only new numbers will be selected as the row indexes.

There is probably a simpler way to all of this, Im still very new to coding / uipath.

If you are referring the first column to be in order, then sort the datatable with any other column. This will sort the table randomly, based on the column values.

I asked the same thing awhile ago here: How to randomize datatable row order using Fisher-Yates (Knutes) shuffle

It’s tough to do a true knutes/fisher-yates shuffle due to the limitation of uipath not having for loops available. It can be emulated in a while loop though instead.

In my case, I had to label each row in the newly randomized order (in case users accidentally re-sorted they could get back to the random order). Because of that I decided to populate an array of integers equal to the amount of rows in the datatable. I then used invoke code to shuffle the integer array, added the shuffled numbers to a new column in the datatable, then sorted on that column ascending.

Here is the function - it receives a datatable, randomizes it, then passes it back out. Note that you can simply delete the ShuffleOrder Column at the end of the function if you don’t want it kept
KnutesShuffle.xaml (11.7 KB)

EDIT: I’ve tested with tables containing 200k rows and 25 columns and it takes ~10 seconds

1 Like