how to sort list dynamically?

I am trying to build different options for sorting on listview. So for example, default is by name and I give user option to change sorting using another field like "rating".
If I sort observablecollection in the viewmodel and raise property changed like below. SfListView is not updated with new Items. What is the best way to achieve this?

  Items = new ObservableCollection<Item>(SelectedSorting.id==0? filteredList.OrderBy(s=>s.name): filteredList.OrderByDescending(s => s.Rating));

   RaisePropertyChanged("Items");

1 Reply

MK Muthu Kumaran Gnanavinayagam Syncfusion Team March 12, 2018 07:18 AM UTC

Hi Emil, 
 
We have checked the reported query “Need to sort list dynamically based on the Selected index” from our side. You can achieve your requirement by maintaining a property in your ViewModel class which is bound to SelectedIndex property of the Picker to monitor the changed index accordingly. Based on the index position, you can change the sort options as like below code example. 
 
Code Example(XAML): 
<Picker SelectedIndex="{Binding MySelectedIndex}" ItemsSource="{Binding PickerItems}" WidthRequest="100" HorizontalOptions="End" VerticalOptions="End"/> 
 
 
Code Example[C#]: 
public int mySelectedIndex;

public
int MySelectedIndex 
{ 
  get 
  { 
    return mySelectedIndex; 
  } 
  set 
  { 
    mySelectedIndex = value; 
    CustomerDetails = new ObservableCollection<Contacts>(mySelectedIndex == 0 ? CustomerDetails.OrderBy(s => s.ContactName) : CustomerDetails.OrderByDescending(s => s.ContactNumber)); 
    RaisePropertyChanged("MySelectedIndex"); 
  } 
} 
 
For your reference, we have attached the sample and you can download it from the below link. 
 
 
Please let us know if you require further assistance. 
 
Regards, 
G.Muthu kumaran. 


Loader.
Up arrow icon