top of page
  • Hina Garg

Sort a list in SXA by using custom sorting options

Updated: Sep 1, 2020


Sitecore has introduced Scriban template in Sitecore 9.3 which is quite amazing to use and I came across a requirement where in I combined Scriban template with a Subitems Sorting option of Sitecore.


Objective: Sort a page list by article date.


Custom Sorting Option: Sitecore provides several OOTB Sub-sorting options that can be used to sort a list in the Sitecore content tree. Few of them are:

  • Created

  • Display Name

  • Logical

  • Reverse

  • Updated

But, if we need to insert a sorting option in this setting, we should be able to achieve it by extending the standard comparer class.


Approach:

1. First, create a custom comparer class in your solution.


Below is an example code demonstrating one such custom comparer class which is introducing an option to sort the articles by article date in descending order.


Example Code:



public class SortingRule : Comparer
    {
        /// <summary>Compares two items.</summary>
        /// <param name="item1">The item1.</param>
        /// <param name="item2">The item2.</param>
        /// <returns></returns>
        protected override int DoCompare(Item item1, Item item2)
        {
            return GetArticleDate(item2).CompareTo(GetArticleDate(item1));
        }

        /// <summary>Gets the article date.</summary>
        /// <param name="item">The item.</param>
        private DateTime GetArticleDate(Item item)
        {
            SortingRuleStatistics sortingRuleStatistics = new SortingRuleStatistics();
            DateTime dateTime = DateTime.MaxValue;
            foreach (Item version in item.Versions.GetVersions(true))
            {
                DateTime articleDate = sortingRuleStatistics.ArticleDate(version);
                if (articleDate != DateTime.MinValue && articleDate.CompareTo(dateTime) < 0)
                    dateTime = articleDate;
            }
            return !(dateTime == DateTime.MaxValue) ? dateTime : DateTime.MinValue;
        }
    }


public class SortingRuleStatistics
    {
        /// <summary>Gets the article date of the source item.</summary>
        /// <value>The article date.</value>
        public virtual DateTime ArticleDate(Item item)
        {
            return GetArticleDateValue(Templates.NewsArticles.Fields.ArticleDate, item);
        }

        /// <summary>Gets the date value.</summary>
        /// <param name="fieldID">The field ID.</param>
        /// <param name="item">The source item.</param>
        /// <returns>The date value.</returns>
        private DateTime GetArticleDateValue(ID fieldID, Item item)
        {
            return 
DateUtil.ToUniversalTime(DateUtil.IsoDateToDateTime(this.GetArticleDateFieldValue(fieldID, item), DateTime.MinValue));
        }

        /// <summary>Gets the field value.</summary>
        /// <param name="fieldId">The field id.</param>
        /// <param name="item">The source item.</param>
        /// <returns></returns>
        private string GetArticleDateFieldValue(ID fieldId, Item item)
        {
            return item.Fields[fieldId].GetValue(false, false) ?? string.Empty;
        }

    }

2. Now go to your sitecore instance and navigate to : /Sitecore/System/Settings/Subitems Sorting in the content tree.

3. Now right click and insert a new item of /System/Child Sorting data template. For the above example code the item inserted is Article Date.

4. Open the new item and in the Type field of Data section enter the Class,Assembly of the custom comparer class created in the solution.

5. Last step is to set this rule on the standard values of the template which is used to create items that need sorting. For our example, it is the Article template.


Create the List to be sorted by using SXA:


Approach:


1. Create a template that would have the required fields to be displayed in the list.

2. Then go to /Presentation/Rendering Variants/Page Content and insert a new variant definition corresponding to your list item.

3. Add a new Scriban template to display the fields of a list item.


Example code:



<div class="row">
{{ for i_child in (sc_query i_item 'query:./descendant::*[@@templatename="Articles Folder"]//*')  }}
	<div class="col-4 get-latest-news-item">
	{{sc_field i_child "Image" }}
        <h4><a href="{{ sc_link i_child }}">{{ i_child.Title }}</a></h4>
	{{sc_field i_child 'ArticleDate' [ [  'format', 'MMMM d, yyyy' ] ]  }}
	{{sc_field i_child "Introduction" }}
	</div>
{{end}}
</div>





107 views0 comments
bottom of page