Sorting posts

Oct 20, 2010 at 3:38 PM

Is there a way to rearrange the post order to either most comments or most viewed? I would like to create a page that basically shows the same thing as default.aspx, but with the posts in one of these two orders.

Oct 22, 2010 at 3:51 PM

Is there a way to add some code maybe to default.aspx.cs that would sort the posts from most comments to least? I am stumped.

Coordinator
Oct 22, 2010 at 5:32 PM

In default.aspx.cs, when the homepage posts are shown, there's this code (line break added to help with wrapping).

PostList1.Posts = posts.ConvertAll(new Converter<Post,
	IPublishable>(delegate(Post p) { return p as IPublishable; }));
If you replace that with the following, it'll sort the posts so the posts with the most number of comments appear first.

List<Post> posts = Post.Posts;
posts.Sort(delegate(Post p1, Post p2)
{
	return p2.Comments.Count.CompareTo(p1.Comments.Count);
});

PostList1.Posts = posts.ConvertAll(new Converter<Post,
	IPublishable>(delegate(Post p) { return p as IPublishable; }));	

 

Oct 22, 2010 at 5:42 PM

Thanks Ben. Can you do something similar for most viewed as well? I have a feeling that would be more difficult.

Coordinator
Oct 22, 2010 at 5:56 PM

The number of times a post is viewed isn't being tracked -- so it would be difficult from that standpoint since we don't know the # of views.

There's a widget and extension that's been created, however, to track the number of views.  Al Nyveldt originally created it here, but it looks like some of the files in the download might be missing.  It looks like the complete download is here.  You could play around with what's there to create what you're looking for.

Dec 15, 2010 at 10:05 PM

My client wants 2 pages basically built for the homepage posts. One for most recent (which would be the default), and one for most comments. I created a new page called default2.aspx and default2.aspx.cs and added the above code to make it so the posts are ordered by most comments. However once that pages has been accessed, it changes to that sorting on default.aspx as well. Is there an easy way around this? I just want default.aspx to sort normally (recent) and another page to sort by most comments. Thanks.