medvedo
medvedo

Reputation: 563

JSON into listview in xamarin forms, does not order in the list after the json

I work with a database (json) that i load into a listview.

My json that I recieve is ordered the way I want it but as group the different categoryID's into different categories it does order itself like the json looks like (ordered with end_date first to last) but instead it loads into the list into the different categories.

This is how I recieve my data:

static public async Task<JObject> getEvents ()
    {
        var httpClientRequest = new HttpClient ();

        var result = await httpClientRequest.GetAsync ("http");

        var resultString = await result.Content.ReadAsStringAsync ();

        var jsonResult = JObject.Parse (resultString);

        //this below does not effect how the list turns out. I have tried it
        /*
        JArray sorted = new JArray (jsonResult ["records"].OrderBy (obj => obj ["end_date"]));

        jsonResult ["records"] = sorted; */


        return jsonResult;

    }

The listview & items:

public class items
    {
        public string eventTitle { get; set;}
        public string endDate { get; set;}
    }

new List <items> theListOne = new List <items> ();
new List <items> theListTwo = new List <items> ();

string idcategory;

async void loadList ()
{

var getItems = await phpApi.getEvents ();

  foreach (var currentitem in getItems["records"]) {

                idcategory = currentitem ["id_events_categories"].ToString (); 


if (idcategory == "1") { 

                    theListOne.Add (new items () {

                        eventTitle = currentitem ["title"].ToString (),
                        endDate = currentitem ["end_date"].ToString (),
                    });
    }

if (idcategory == "2") { 

                    theListTwo.Add (new items () {

                        eventTitle = currentitem ["title"].ToString (),
                        endDate = currentitem ["end_date"].ToString (),
                    });     
                  }

        }

var result = theListOne.Concat(theListTwo);

eventsList.ItemsSource = result; //eventsList is my listview x:name

}

So now in the list, the items stack themselves up in their respective category (1 & 2) and not via the JSON-order.

How could I adjust this so the items orderes the same way as the json? I want to order the list after "end_date" just as the json is.

My json looks like this and I have ordered it via the end_date but as I mentioned above. It doesn't order itself in the listview the same away after I separate them into different categories.

UPDATE: IT orderes but only on that particular list and not together like I want it to. So theListOne orders itself after the dates but only within the items in theListOne and the same thing with theListTwo.

{
  status: "ok",
    records: [
                { 
                  title: "Test",
                  end_date: "2016-05-22 16:00:00",
                  id_events_categories: "1",
                }

            ]
}

Upvotes: 0

Views: 436

Answers (1)

hvaughan3
hvaughan3

Reputation: 11105

Is this what you want:

var result = theListOne.Concat(theListTwo);

eventsList.ItemsSource = result.OrderByDescending(x => DateTime.Parse(x)).ToList();

*Edit: Sorry I am an idiot. Try this:

var result = theListOne.Concat(theListTwo);

eventsList.ItemsSource = result.OrderByDescending(x => DateTime.Parse(x.endDate)).ToList();

Upvotes: 1

Related Questions