0
votes

How would one go about sorting a Custom Listview on xamarin forms, say if it had a date and time jason data how would you make it so that the newest date shows up at the top

Here is my jason deserialization

public class RootObject
{

    public List<Feed> Feeds { get; set; }
}

public class Channel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Latitude { get; set; }
    public string Longitude { get; set; }
    public string Field1 { get; set; }
    public DateTime Created_at { get; set; }
    public DateTime Updated_at { get; set; }
    public string Elevation { get; set; }
    public int Last_entry_id { get; set; }
}

public class Feed
{
    public DateTime Created_at { get; set; }
    public int Entry_id { get; set; }
    public string Field1 { get; set; }
}


public partial class DripPage : TabbedPage
{
    void Handle_Refreshing(object sender, System.EventArgs e)
    {
        postsListView.ItemsSource = _data;
        postsListView.EndRefresh();
    }

    private const string Url = "https://thingspeak.com/channels/301726/field/1.json";
    private HttpClient _client = new HttpClient();
    private ObservableCollection<Feed> _data;


    public DripPage()
    {
        InitializeComponent();
    }

    protected override async void OnAppearing()
    {
        var content = await _client.GetStringAsync(Url);
        var data = JsonConvert.DeserializeObject<RootObject>(content);

        _data = new ObservableCollection<Feed>(data.Feeds);
        postsListView.ItemsSource = _data;


        base.OnAppearing();
    }

    void OnAdd(object sender, System.EventArgs e)
    {
    }

    void OnUpdate(object sender, System.EventArgs e)
    {
    }

    void OnDelete(object sender, System.EventArgs e)
    {
    }
}

}

Just need the dateandtime element to be sorted from newest to oldest Cheers

1

1 Answers

0
votes

Order the Feeds collection using LINQ (OrderByDescending):

_data = new ObservableCollection<Feed>(data.Feeds.OrderByDescending(x => x.Created_at));