Sorting a collection by multiple fields

后端 未结 4 732
甜味超标
甜味超标 2020-12-21 07:17

I need to sort a collection. For example, I have

Austin 12/3/2012   
Sam 100 12/3/2012   
Sam 200 14/3/2012   
Bubly 300 12/3/2012   
Bubly 300 15/3/2012  
         


        
4条回答
  •  不思量自难忘°
    2020-12-21 07:54

    I would create a class to hold your information

    public class NameDate
    {
        private string name;
        private DateTime date;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public DateTime Date
        {
            get { return date; }
            set { date = value; }
        }
    }
    

    I would then populate a List to hold your items. When this is done, to sort the items you can use LINQ

    List someList = new List();
    someList = GetNameDateList();
    var orderedByNameList = someList.OrderBy(e => e.Name);
    var orderedByDateTimeList = someList.OrderBy(e => e.Date);
    

    I hope this helps.

提交回复
热议问题