Developer forum

Forum » CMS - Standard features » Order by random in item publisher list/news list

Order by random in item publisher list/news list

Hans Ravnsfjall
Reply

Hi

I created at feature request on this a while back, but don´t think this has been added as a functionality.

The request was to make it possible to order by random when showing a newslist. This would also be useful in Item Publisher list.

Till now, we have solved this with jQuery/Javascript, but that is not a satisfying solution long term.

Anybody know if this is or will be possible soon?

 

 


Replies

 
Mikkel Ricky
Reply
This post has been marked as an answer

The only way to do this right now is to use a Razor template and a shuffle function, e.g.

@functions {
// @see http://stackoverflow.com/a/1262619
public static void Shuffle<T>(IList<T> list)
{
    Random rng = new Random();
    int n = list.Count;
    while (n > 1) {
        n--;
        int k = rng.Next(n + 1);
        T value = list[k];
        list[k] = list[n];
        list[n] = value;
    }
}
}

@{
// Get the items (or News)
var items = GetLoop("ItemPublisher:Items.List");
Shuffle(items);
}

@foreach (var item in items) {
    …
}

Best regards,
Mikkel

Votes for this answer: 1
 
Hans Ravnsfjall
Reply

Thanx :)

This works perfectly.

Would be nice if you could add this as a sort option in future versions. Should be an easy task to implement.

 

You must be logged in to post in the forum