I’ve been searching the forum and could only find solutions to random sorting a gallery, and getting random items from a dataset. Getting random items from a dataset is by far the closest answer to my inquiry, however there is a possibility of getting a repeated item which is not what I want. Is there any way to random sort a dataset? Any help would be greatly appreciate.
Just use a shuffle algorithm (such as Fisher–Yates shuffle ) on your data and assign the shuffled items array to the gallery using $w(“#myGallery”).items assignment.
See:
I have come across a post that talks about random sorting for gallery, but I’m not using a gallery. My items are just text data, and I’m using a dataset, hence need help in doing random sorting for dataset.
@buzzzybeebee same.
Get the collection items, then shuffle them as described in the link I posted,
@jonatandor35 can this work on dynamic dataset?
This is the code I played around for the repeater (which works), but it’s not what I’m looking for.
This is what I’m working on for my dynamic dataset, but I’m stuck because I don’t know how to pass the “shuffled array” back to my dynamic dataset since there is no such thing as $w ( “#dynamicDataset” ).data. Really appreciate if you can help point out what I’m missing or doing wrong. Thanks in advanced.
@buzzzybeebee You can’t and you don’t need to set the shuffled data in the dataset. Just use the shuffled data for whatever you need: repeaters, galleries, tables, whatever.
@jonatandor35 I did try. But the problem is, I’m only displaying one item per page (hence the reason for using dynamic pages / datasets), so if I use the shuffled data on a repeater and set it to display only one item, it will always be showing the same “unsorted” default item. In your opinion, does that mean there is NO WAY around this to possibly make it work?
@buzzzybeebee If it’s a dynamic page that’s filtered to show a single item, then all the shuffling is not relevant (because it shows the page that is relevant to the specific item).
I you wish to have a static page and show a single random item, then install the data-helpers Velo package and use it to pick up a random item from the database.
The shuffling is only relevant if you show several items on the page and wish to sort them randomly.
@jonatandor35 That’s great! At least it’s not a dead end. I will try using the data-helpers velo package. Thanks for your tips & help!
Hello,
I would also like to randomly sort something on my overview page. Repeater is connected to Dataset and sorted by Created Date.
Can anyone tell me how to adjust the code and which positions are changed? I am absolute beginner in javascript.
My code now looks like this overall:
import wixData from ‘wix-data’ ;
function shuffleArray ( inputArray ){
inputArray . sort (()=> Math . random () - 0.5 );
}
$w ( “#teamDataset” ). onReady (()=>{
let items = $w ( “#teamRepeater” ). data ;
items = shuffleArray ( items );
$w ( “#teamRepeater” ) . data = items ;
});
no one has an idea?
Hey there - having a discussion here in a similar vein. Welcome to jump in. https://www.wix.com/velo/forum/coding-with-velo/how-do-i-assign-the-first-50-items-to-a-repeater. https://www.wix.com/velo/forum/coding-with-velo/how-do-i-assign-the-first-50-items-to-a-repeater .
I am finding the array sort very slow, so thinking of another way that it can be done in the database. At this point, my thought is along assigning a random number to reach row that is in line with the number of rows in the collection, then just order by and LIMIT. Any random number clashes should be taken care of by the ORDER. Who knows if it will work. In theory it sounds great, but when I code my theories they all turn to custard.