BlogEngine.NET comes with a nice blogroll control. Here's what mine looks like:
One thing I don't like about it is that the entries are not sorted. If you're using XML as your underlying data store, you'll find your blogroll entries in the "blogroll.xml" file. It's easy enough to sort this file manually as long as you only have a handful of blogroll items, but clearly that's not the best, long-term solution.
Instead, I'm going to show how to modify the Blogroll.cs control to automatically sort your blogroll items prior to display. You can download the modified Blogroll.cs file at the bottom of this post.
I had a number of approaches in mind before undertaking this task. The one thing I really wanted to stick to was to avoid changing the existing Blogroll class. I found during my recent upgrade to BlogEngine.NET 1.4.5 that it's best to not mess with the stock items. So, with that in mind, here's some of the ideas I tried (and the reasons they didn't work):
1. Extend the existing control with a control of my own.
Unfortunately this did not go so well. The Blogroll class is locked down pretty tight, with plenty of "private" access modifiers to work around. I could change these to "protected", but that sort of defeats the purpose of leaving the existing class intact.
2. Delete the existing Blogroll.cs and substitute it with my own "ItsCodingTimeBlogroll.cs"
I thought this was the next best thing, but I was quickly foiled by all of the dependencies that remained to the old Blogroll.cs. This makes sense considering Blogroll.cs is a core class of the BlogEngine.NET product.
3. Just modify Blogroll.cs
The changes were fairly minor, so, given the above roadblocks, I decided to go this route. I don't particularly like this approach (I would have liked to have gone with option #1), but the BlogEngine.NET team did not make this real easy, IMHO.
There are probably other approaches, but those are the ones which came to mind immediately.
In the end, using idea #3 above, this is what I did:
1. In Blogroll.cs (in App_Code\Controls) add a new method called "SortBlogs"
1: private void SortBlogs ()
2: {
3: try
4: {
5: ArrayList.Adapter (_Items).Sort ();
6: }
7: catch (Exception)
8: {
9: // log error if you like
10: }
11:
12: return;
13: }
2. Add a call to the new method in "CreateList()"
I added "SortBlogs()" as the last step in the "if File.Exists (fileName)" section, like:
3. Inherit RssItem from IComparable
Now, locate the "RssItem" class. Inherit from "IComparable":
1: private class RssItem : IComparable
2: {
3: ...
4. Add a CompareTo method
Add a new method, "CompareTo", to the "RssItem" class:
1: public int CompareTo (object obj)
2: {
3: if (obj is RssItem)
4: {
5: RssItem rssItem = (RssItem) obj;
6:
7: return (this.Name.CompareTo (rssItem.Name)); // sort by "Name"
8: }
9: throw new ArgumentException (
10: string.Format ("Cannot compare RssItem to {0}",
11: obj.GetType().ToString ())
12: );
13: }
Note that I'm sorting on the "Name" field. You can sort on any of the field names in the XML file, but "Name" seemed the most intuitive.
5. Deploy Blogroll.cs to your web site
Copy Blogroll.cs out to your "App_Code\controls" folder on your web site.
6. Verify
You should have a sorted Blogroll now:
Download: Blogroll.zip.
Source(s): I Think, Therefore I Code: How to sort a generic IList<T>