I have an array of CDTs say questions with question structured as:
{
id: 1,
statement: "Text string of question",
options: {List of Options}
}
Like multiple values like the above.
I would like to shuffle this array of questions randomly so that every time I print this array the questions are in no particular order and are different from last time. Example if I pick the array first time it had an order:
1, 4, 2, 5, 3
But next time I pick same array it should be ordered randomly like:
3, 5, 1, 4, 2
Also is there any possible way to implement Rand() ordering in the query like we do in SQL?
Discussion posts and replies are publicly visible
There are various ways to do this, but anything you do you'll have to build yourself.
A quick-and-dirty way I just thought of:
Thanks Mike,
This was a really innovative way to sort and shuffle a large array at low perofromance cost. But I feel that since we have the functionality of fetching randomly sorted records direct from SQL query in MySQL, Appian should incorporate a way to use that functionality as it will save a great coding time and performance load will be reduced.
Something like that would need to be built into the a!queryEntity function or one of its components - you could always file a feature request ticket with Appian Support, however I wouldn't expect it to be prioritized anytime soon without a strong use case on your end.
Out of boredom I went ahead and implemented a standalone function where you input the desired number of items and it returns a randomized list of integer indices 1...N, where we're guaranteed to have no duplicates or missing numbers. This integer array could then be used to index into any particular array in a randomized manner.
a!localVariables( local!targetSize: 25, local!dict: a!forEach( enumerate(local!targetSize), a!map( integer: fv!index, sort: rand() ) ), local!sorted: todatasubset( local!dict, a!pagingInfo( startIndex: 1, batchSize: -1, sort: a!sortInfo(field: "sort", ascending: true()) ) ), local!sorted.data.integer )