I am new to Xamarin and trying to sort ObservableCollection by using a string which I am getting on picker selection :
Model and ViewModel classes
public class TestListModel
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string SSN { get; set; }
public string Gender { get; set; }
public string DOB { get; set; }
public string PhoneNumber { get; set; }
public string Address { get; set; }
public string State { get; set; }
public string City { get; set; }
}
public class TestViewModel
{
public ObservableCollection TestListModel { get; set; }
public TestViewModel()
{
TestModel = new ObservableCollection<TestModel>
{
new TestModel
{
FirstName = "John",
LastName ="Pasteka",
SSN="xxxxxxx",
Gender="Male",
DOB="02/09/1945",
PhoneNumber="xxxxxxx",
Address="xxxxxxx",
State="TX",
City="ROSENBERG"
},
new TestModel
{
FirstName = "peter",
LastName ="ETZEL",
SSN="xxxxxxx",
Gender="Male",
DOB="02/21/1945",
PhoneNumber="xxxxxxxxxxxxxx",
Address="xxxxxxxxxxx",
State="TX",
City="HOUSTON"
},
new TestModel
{
FirstName = "josh",
LastName ="STEVENS",
SSN="xxxxxxxxxxx",
Gender="Female",
DOB="01/05/1930",
PhoneNumber="xxxxxxxxxxx",
Address="xxxxxxxxxxx",
State="TX",
City="RICHMOND"
},
new TestModel
{
FirstName = "Rock",
LastName ="PARRA",
SSN="xxxxxxxxxxx",
Gender="Male",
DOB="10/09/2004",
PhoneNumber="xxxxxxxxxxx",
Address="xxxxxxxxxxx. ",
State="TX",
City="PATTISON"
},
new TestModel
{
FirstName = "aron",
LastName ="USMANI",
SSN="xxxxxxxxxxx",
Gender="Female",
DOB="08/22/1994",
PhoneNumber="xxxxxxxxxxx",
Address="xxxxxxxxxxx",
State="TX",
City="ROSENBERG"
}
};
}
}
I have a picker with values First Name, Last Name, Gender, SSN, Address.
private void Picker_OnSelectedIndexChanged(object sender, EventArgs e)
{
if (PickerCtl != null && PickerCtl.SelectedIndex <= PickerCtl.Items.Count)
{
**Here I am getting filter selected by the user and i want to use this string to sort the items**
var selecteditem = PickerCtl.Items[PickerCtl.SelectedIndex];
var test = selecteditem.Split(' ');
string newtest = "";
if (test.Length > 1)
{
newtest = test[0] + test[1];
}
else
{
newtest = test[0];
}
**Here I have tried the solution:**
System.Reflection.PropertyInfo prop = typeof(string).GetProperty(newtest);
TestViewModel Iitem = new TestViewModel();
var result = Iitem.TestListModel.OrderBy(x => prop.GetValue(x, null));
**After sorting I want to show the sorted result to the user in listview**
lvSearch.ItemsSource = result;
}
}