How to sort the group items dynamically in Xamarin.Forms ListView (SfListView)?
You can sort the group items at run time in Xamarin.Forms SfListView.
XAML
The Button is used to sort the items at run time.
<StackLayout> <Button x:Name="sortButton" Text="Sort the items" HeightRequest="50"/> <syncfusion:SfListView x:Name="listView" ItemSize="60" ItemsSource="{Binding ContactsInfo}"> <syncfusion:SfListView.ItemTemplate > <DataTemplate> <Grid x:Name="grid"> <Grid.ColumnDefinitions> <ColumnDefinition Width="70" /> <ColumnDefinition Width="*" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <Image Source="{Binding ContactImage}" VerticalOptions="Center" HorizontalOptions="Center" HeightRequest="50" WidthRequest="50"/> <Grid Grid.Column="1" RowSpacing="1" Padding="10,0,0,0" VerticalOptions="Center"> <Label LineBreakMode="NoWrap" TextColor="#474747" Text="{Binding ContactName}"/> <Label Grid.Row="1" Grid.Column="0" TextColor="#474747" LineBreakMode="NoWrap" Text="{Binding ContactNumber}"/> </Grid> <Label Text="{Binding ContactType}" Grid.Column="2" Padding="0,0,10,0"/> </Grid> </DataTemplate> </syncfusion:SfListView.ItemTemplate> </syncfusion:SfListView> </StackLayout>
C#
In the Button.Clicked event, add DataSource.SortDescriptor with the required property. Refresh the DataSource using the DataSource.Refresh method to reflect the changes in the UI.
public class Behavior : Behavior<ContentPage> { SfListView ListView; Button SortButton; protected override void OnAttachedTo(ContentPage bindable) { ListView = bindable.FindByName<SfListView>("listView"); ListView.DataSource.GroupDescriptors.Add(new GroupDescriptor() { PropertyName = "ContactType" }); SortButton = bindable.FindByName<Button>("sortButton"); SortButton.Clicked += SortButton_Clicked; base.OnAttachedTo(bindable); } private void SortButton_Clicked(object sender, EventArgs e) { ListView.DataSource.SortDescriptors.Add(new SortDescriptor() { PropertyName = "ContactName", Direction = ListSortDirection.Ascending, }); ListView.DataSource.Refresh(); } }
Conclusion
I hope you enjoyed learning about how to sort the group items dynamically in Xamarin.Forms ListView (SfListView).
You can refer to our Xamarin.Forms ListView feature tour page to know about its other groundbreaking feature representations. You can also explore our Xamarin.Forms ListView 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 forums, Direct-Trac, or feedback portal. We are always happy to assist you!