简体   繁体   中英

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!

you may use reflection

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM