As the sort order field isn't returned by default the best way to handle this is to add a add a new computed field to the index. This will then be available to query by in Solr.
1. Computed Index Field
Create this class within your Sitecore project:
public class SortOrderField : IComputedIndexField
{
public object ComputeFieldValue(IIndexable indexable)
{
int DefaultSortOrderValue = 0;
var item = (Item)(indexable as SitecoreIndexableItem);
if (item == null) return null;
if (string.IsNullOrEmpty(item[Sitecore.FieldIDs.Sortorder]))
{
return DefaultSortOrderValue;
}
int sortOrder;
return int.TryParse(item[Sitecore.FieldIDs.Sortorder], out sortOrder)
? sortOrder
: DefaultSortOrderValue;
}
public string FieldName { get; set; }
public string ReturnType { get; set; }
}
2. Config for Computed Field
Add this config patch file (or merge it into an existing search patch file):
<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
<sitecore>
<contentSearch>
<indexConfigurations>
<defaultSolrIndexConfiguration type="Sitecore.ContentSearch.SolrProvider.SolrIndexConfiguration, Sitecore.ContentSearch.SolrProvider">
<fields hint="raw:AddComputedIndexField">
<field fieldName="sortorder" returnType="int">MyCustom.Search.Indexing.SortOrderField, MyCustom.Search</field>
</fields>
</indexConfigurations>
</contentSearch>
</sitecore>
</configuration>
3. Search Result Item Model
Add the sort order into either your base Search Result Item model so it's available for all search result types or a specific model like so:
public class ContentSearchItem : SearchResultItem
{
[IndexField("_displayname")]
public string DisplayName { get; set; }
[IndexField("__smallupdateddate")]
public DateTime LastUpdated { get; set; }
[IndexField("sortorder")]
public int SortOrder { get; set; }
}
4. Update you search query
You should now be able to update your search query to include the ordering by sortorder like so:
using (var context = ContentSearchManager.CreateSearchContext((SitecoreIndexableItem) indexItem))
{
var searchResultItems = context.GetQueryable<ContentSearchItem>()
.OrderBy(x => x.SortOrder)
}
5. Rebuild your indexes
You will need to re-build your web index (and master if you want to see the changes on CA) in order for the new sortorder field to show up in the index.
You may also need to re-order content in Content Editor so that the sortorder field value is set (or perhaps use Sitecore Powershell to do this for you).
There is more info here and additional steps you may need to carry out depending on your situation (thanks to Marta Imos-Merska for her post):
https://sitecore.unic.com/2016/07/07/contentsearch-instead-of-sitecore-queries-with-content-tree-order