How to Sort a List by a property in the object

前端 未结 20 1999
醉梦人生
醉梦人生 2020-11-21 08:25

I have a class called Order which has properties such as OrderId, OrderDate, Quantity, and Total. I have a l

相关标签:
20条回答
  • 2020-11-21 08:29

    From performance point of view the best is to use a sorted list so that data is sorted as it is added to result. Other approaches need at least one extra iteration on data and most create a copy of data so not only performance but memory usage will be affected too. Might not be an issue with couple of hundreds of elements but will be with thousands, especially in services where many concurrent requests may do sorting at the same time. Have a look at System.Collections.Generic namespace and choose a class with sorting instead of List.

    And avoid generic implementations using reflection when possible, this can cause performance issues too.

    0 讨论(0)
  • 2020-11-21 08:30

    An improved of Roger's version.

    The problem with GetDynamicSortProperty is that only get the property names but what happen if in the GridView we use NavigationProperties? it will send an exception, since it finds null.

    Example:

    "Employee.Company.Name; " will crash... since allows only "Name" as a parameter to get its value.

    Here's an improved version that allows us to sort by Navigation Properties.

    public object GetDynamicSortProperty(object item, string propName)
        {
            try
            {                 
                string[] prop = propName.Split('.'); 
    
                //Use reflection to get order type                   
                int i = 0;                    
                while (i < prop.Count())
                {
                    item = item.GetType().GetProperty(prop[i]).GetValue(item, null);
                    i++;
                }                     
    
                return item;
            }
            catch (Exception ex)
            {
                throw ex;
            }
    
    
        } 
    
    0 讨论(0)
  • 2020-11-21 08:32

    // Totally generic sorting for use with a gridview

    public List<T> Sort_List<T>(string sortDirection, string sortExpression, List<T> data)
        {
    
            List<T> data_sorted = new List<T>();
    
            if (sortDirection == "Ascending")
            {
                data_sorted = (from n in data
                                  orderby GetDynamicSortProperty(n, sortExpression) ascending
                                  select n).ToList();
            }
            else if (sortDirection == "Descending")
            {
                data_sorted = (from n in data
                                  orderby GetDynamicSortProperty(n, sortExpression) descending
                                  select n).ToList();
    
            }
    
            return data_sorted;
    
        }
    
        public object GetDynamicSortProperty(object item, string propName)
        {
            //Use reflection to get order type
            return item.GetType().GetProperty(propName).GetValue(item, null);
        }
    
    0 讨论(0)
  • 2020-11-21 08:33

    Simplest way to order a list is to use OrderBy

     List<Order> objListOrder = 
        source.OrderBy(order => order.OrderDate).ToList();
    

    If you want to order by multiple columns like following SQL Query.

    ORDER BY OrderDate, OrderId
    

    To achieve this you can use ThenBy like following.

      List<Order> objListOrder = 
        source.OrderBy(order => order.OrderDate).ThenBy(order => order.OrderId).ToList();
    
    0 讨论(0)
  • 2020-11-21 08:33
    var obj = db.Items.Where...
    
    var orderBYItemId = obj.OrderByDescending(c => Convert.ToInt32(c.ID));
    
    0 讨论(0)
  • 2020-11-21 08:33

    Make use of LiNQ OrderBy

    List<Order> objListOrder=new List<Order> ();
        objListOrder=GetOrderList().OrderBy(o=>o.orderid).ToList();
    
    0 讨论(0)
提交回复
热议问题