Fix alphabetic sorting of pages in EPiServer CMS 5

I wrote earlier about the annoying fact that alphabetic sorting of child pages in EPiServer CMS 5 is controlled by the collation settings in your database server and suggested that it could be fixed. My first solution that tinkers with database settings had side effects and should not be used.

How to fix sorting of children in EPiServer CMS 5

//Hook FirstBeginRequest Application_Start event in Global.asax.cs 
void InitializationModule_FirstBeginRequest(object sender, EventArgs e)
{
    DataFactory.Instance.FinishedLoadingChildren += EPDataFactory_FinishedLoadingChildren;
}

private void EPDataFactory_FinishedLoadingChildren(object sender, EPiServer.ChildrenEventArgs eventArgs)
{
    if ((eventArgs.Children.Count > 0) && (((int)DataFactory.Instance.GetPage(eventArgs.PageLink)["PageChildOrderRule"]) == 3))
    {
        FilterPropertySort comparer = new FilterPropertySort("PageName");
        comparer.CompareInfo = ContentLanguage.PreferredCulture.CompareInfo;
        eventArgs.Children.Sort(comparer);
    }
}

Just add the code above to your Global.asax.cs file and add the hook to FirstBeginRequest event in Application_Start. It will apply sorting of children that is culture sensitive and changes according to the current language.

NB! This fix is not needed in EPiServer CMS 6 because it is already built-in.