Programmatically enable sorting for Navigation SharePoint

I have a site collection with the SharePoint Server Publishing Infrastructure feature enabled on the site collection and sites with the SharePoint Server Publishing site feature activated.

Go to Site Actions => Site Settings => Look and Feel => Navigation => Sorting.

LookandFeel.png

Programmatically Enable/Disable Sorting for navigation



using (SPSite siteCollection = new SPSite("http://servername:1111/sites/sample"))
{
using (SPWeb web = siteCollection.RootWeb)
{
PublishingWeb publishingWeb = PublishingWeb.GetPublishingWeb(web);
// Sorting
// Do sorting
publishingWeb.Update();
}
}

########################################################################################################

// Sort manually


SortManually.png


publishingWeb.Navigation.OrderingMethod =
OrderingMethod.Manual;



//Sort manually with automatic paging
SortManuallywithPagesSorted.png

publishingWeb.Navigation.OrderingMethod =
OrderingMethod.ManualWithAutomaticPageSorting;
//sort automatically

SortAutomatic.png

publishingWeb.Navigation.OrderingMethod =
OrderingMethod.Automatic;
//Sort by title
publishingWeb.Navigation.AutomaticSortingMethod =
AutomaticSortingMethod.Title;
//Sort by created date
publishingWeb.Navigation.AutomaticSortingMethod =
AutomaticSortingMethod.CreatedDate;
//Sort by last modified date
publishingWeb.Navigation.AutomaticSortingMethod =
AutomaticSortingMethod.LastModifiedDate;
//Sort by ascending order
publishingWeb.Navigation.SortAscending =
true;
//Sort by descending order
publishingWeb.Navigation.SortAscending =
false;