6

I know that we can randomly sort a DataList with the following:

$example = Example::get()->sort('RAND()');

But when I try to randomly sort an ArrayList it doesn't work. I can sort an ArrayList by ID DESC, but not with RAND().

Is there a way to make an ArrayList randomly sort its items?

Example:

public function AllTheKits() {
    $kits = Versioned::get_by_stage('KitsPage', 'Live');
    $kitsArrayList = ArrayList::create();

    foreach ($kits as $kit) {
        if ($kit->MemberID == Member::currentUserID()) {
            $kitsArrayList->push($kit);
        }
    }
    return $kitsArrayList;      
}

In a page:

public function getKitsRandom() {
    return $this->AllTheKits()->sort('RAND()');
}

This does not work in a template with <% loop KitsRandom %>

3dgoo
  • 15,716
  • 6
  • 46
  • 58
StefGuev
  • 639
  • 4
  • 16

2 Answers2

4

Not really. This is the best workaround I can come up with:

foreach($myArrayList as $item) {
    $item->__Sort = mt_rand();
}

$myArrayList = $myArrayList->sort('__Sort');
UncleCheese
  • 1,584
  • 9
  • 14
3

You could randomly sort the DataList before you loop over it, instead of trying to randomly sort the ArrayList:

public function AllTheKits($sort = '') {
    $kits = Versioned::get_by_stage('KitsPage', 'Live', '', $sort);
    $kitsArrayList = ArrayList::create();

    foreach ($kits as $kit) {
        if ($kit->MemberID == Member::currentUserID()) {
            $kitsArrayList->push($kit);
        }
    }
    return $kitsArrayList;      
}

public function getKitsRandom() {
    return $this->AllTheKits('RAND()'));
}

As a side note, you can filter the original DataList to fetch KitsPages that relate to this MemberID in the Versioned::get_by_stage call:

public function AllTheKits($sort = '') {
    $kits = Versioned::get_by_stage(
        'KitsPage',
        'Live',
        'MemberID = ' . Member::currentUserID(),
        $sort
    );
    $kitsArrayList = ArrayList::create($kits);

    return $kitsArrayList;      
}

You could also just do this:

return KitsPage::get()->filter('MemberID', Member::currentUserID())->sort('RAND()');

When you are viewing the live site this will only get the live KitPages.

3dgoo
  • 15,716
  • 6
  • 46
  • 58