Articles in this section
Category / Section

How to sort the items by datetime property along with Grouping in Xamarin ListView?

1 min read

Xamarin.Forms ListView provides support to sort the items along with Grouping based on DateTime property.

Grouping with sorting by Year’s

Sort the items along with grouping using KeySelector based on retuning the year value of data-time property.

Below code snippet directs you to sort items along with group header in ascending order based on the DateTime property.

XAML

<ContentPage xmlns:listView="clr-namespace:Syncfusion.ListView.XForms;assembly=Syncfusion.SfListView.XForms"
             xmlns:dataSource="clr-namespace:Syncfusion.DataSource;assembly=Syncfusion.DataSource.Portable">
  <ContentPage.Content>
    <listView:SfListView x:Name="listView" ItemsSource="{Binding Items}" ItemSize="50">
      <listView:SfListView.GroupHeaderTemplate>
        <DataTemplate>
          <Label Text= "{Binding Key}" BackgroundColor="Teal" FontAttributes="Bold" TextColor="White"/>
        </DataTemplate>
      </listView:SfListView.GroupHeaderTemplate>
    </listView:SfListView>
  </ContentPage.Content>
</ContentPage>

 

C#

public partial class MainPage : ContentPage
{
   public MainPage()
   {
       InitializeComponent();   
            
       listView.DataSource.GroupDescriptors.Add(new GroupDescriptor()
       {
           PropertyName = "DateOfBirth",
           KeySelector = (object obj1) =>
           {
               var item = (obj1 as Contacts);
               return item.DateOfBirth.Year;
           },
       });
 
       this.listView.DataSource.SortDescriptors.Add(new SortDescriptor()
       {
           PropertyName = "DateOfBirth",
           Direction = ListSortDirection.Ascending
       });
   }
}

 

Grouping with sorting by Month and Year

Create the groups using KeySelector based on retuning the year and month value of data-time property along with sorting the items and groups are sorted based on year with months of date-time property by using Comparer.

C#

class CustomGroupComparer : IComparer<GroupResult>, ISortDirection
{
   public CustomGroupComparer()
   {
       this.SortDirection = ListSortDirection.Ascending;
   }
 
   public ListSortDirection SortDirection
   {
       get;
       set;
   }
 
   public int Compare(GroupResult x, GroupResult y)
   {
       DateTime xvalue = Convert.ToDateTime(x.Key);
       DateTime yvalue = Convert.ToDateTime(y.Key);
       // Group results are compared and return the SortDirection
       if (xvalue.CompareTo(yvalue) > 0)
           return SortDirection == ListSortDirection.Ascending ? 1 : -1;
       else if (xvalue.CompareTo(yvalue) == -1)
           return SortDirection == ListSortDirection.Ascending ? -1 : 1;
       else
           return 0;
   }
}

 

C#

public partial class MainPage : ContentPage
{
   public MainPage()
   {
       InitializeComponent();
       listView.DataSource.GroupDescriptors.Add(new GroupDescriptor()
       {
           PropertyName = "DateOfBirth",
           KeySelector = (object obj1) =>
           {
               var item = (obj1 as Contacts);
               return item.DateOfBirth.Month + "/" + item.DateOfBirth.Year;
           },
           Comparer = new CustomGroupComparer()
       });
 
       this.listView.DataSource.SortDescriptors.Add(new SortDescriptor()
       {
           PropertyName = "DateOfBirth",
           Direction = ListSortDirection.Ascending
       });
   }
}

 

The following screenshot shows the sorting along with grouping based on year’s and month/year.

Click here to download the sample.


Conclusion

I hope you enjoyed learning about how to sort the items by datetime property along with Grouping in Xamarin ListView.

You can refer to our Xamarin ListView feature tour page to know about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forumsDirect-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied