user2320462
user2320462

Reputation: 259

order list of objects by date

I have the following:

    List<object> myList = new List<object>();
    myList.orderBy(x => x.Date);

I need to order myList by date, but because it's a list of objects I'm having trouble re-ordering it.

I believe I can do this using IEnumerable:

    IEnumerable<object> receivedData = new List<object>();

but I'm not sure how to order it. Any tips? Thanks!

Upvotes: 1

Views: 88

Answers (2)

Arran
Arran

Reputation: 25056

You will need to cast it into the class you want. object doesn't have a property called Date. Your class does.

You can do this in a few ways, one way is using the as keyword.

Upvotes: 1

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60493

you may use reflection

var orderedList = myList
                 .OrderBy(x => x.GetType().GetProperty("Date").GetValue(x, null)).ToList();

Upvotes: 5

Related Questions