Order Post Catagories by Alpha

Jan 8, 2011 at 4:41 PM

When you are editing or creating a post the list of possible categories is not in alpha order.  It would be easier to use if it was in alpha order.  I think the code used to build that list is below.  How would it need to be amended to sort the list by alpha?

        /// <summary>
        /// The bind categories.
        /// </summary>
        private void BindCategories(Guid postId)
        {
            string catHtml = "";
            var post = postId == Guid.Empty ? null : Post.GetPost(postId);

            foreach (var cat in Category.Categories)
            {
                string chk = "";
                if(post != null && post.Categories.Contains(cat))
                    chk = "checked=\"checked\"";

                catHtml += string.Format("<input type=\"checkbox\" {0} id=\"{1}\">", chk, cat.Id);
                catHtml += string.Format("<label>{0}</label><br/>", Server.HtmlEncode(cat.Title));
            }
            cblCategories.InnerHtml = catHtml;
        }

Jan 8, 2011 at 6:31 PM

You need to modify BlogProvider.FillCategories.  If you're using DbBlogProvider, you need to add 'ORDER BY' to the sql statment in: DbBlogProvider.FillCategories

Some like converting:

                    using (var cmd = conn.CreateTextCommand(string.Format("SELECT CategoryID, CategoryName, description, ParentID FROM {0}Categories ", this.tablePrefix)))

to:

                   using (var cmd = conn.CreateTextCommand(string.Format("SELECT CategoryID, CategoryName, description, ParentID FROM {0}Categories ORDER BY uppercase)CategoryName)", this.tablePrefix)))

 

 

Jan 8, 2011 at 7:00 PM

I implemented your change suggestion and then realized that I am not using the DbBlogProvider.  I am using the XML files for storing my data.  With that being the case, your suggestion would not work, correct?

Thanks for your help

Jan 8, 2011 at 9:53 PM

I'm afraid I was talking rubbish. In fact, categories are sorted after loading. the problems is they are not resorted after you add or rename.

I guess the workaround for now is to restart your website.

Cheers

 

Jan 9, 2011 at 8:13 AM

To resolve the issue you need the changes to Providers\XmlProviders\category.cs

in InsertCategory method: add categories.Sort(); after categories.Add(Category);

in UpdateCategory method add categories.Sort(); after categories.Add(Category)

It was rather simple fix after all. Please let us know how it worked.

Cheers

 

 

Jan 9, 2011 at 6:02 PM

That seems to have done it.  One thing I notied is that sub-categories are listed under their parent in alpha order.

Thanks

Jan 9, 2011 at 6:35 PM
guynethery wrote:

That seems to have done it.  One thing I notied is that sub-categories are listed under their parent in alpha order.

Thanks

As designed! Categories are sorted by CompleteTitle, wich means: including parent (if any). 

Glad it worked.

Cheers