【问题标题】:How to Sort a list by field如何按字段对列表进行排序
【发布时间】:2011-10-24 09:03:23
【问题描述】:

大家好 4 天

我有一个对象列表

我喜欢的对象

Product = "iPhone"; 
Category = "SmartPhone";

Product = "HP"; 
Category = "PC";

Product = "HTC"; 
Category = "SmartPhone";

然后我将每个对象插入到我的测试中,就像这样

List<Myobject> MyList = new List<Myobject>();

现在我需要按类别对 MyList 进行排序/排序

因为我需要我的列表先显示 SmartPhone 类别然后显示其他

【问题讨论】:

    标签: c# .net linq list c#-4.0


    【解决方案1】:

    您可以使用List.Sort

    l.Sort((p, q) => p.Category.CompareTo(q.Category));
    

    与 LINQ OrderBy 相比的优势在于,您将就地订购列表,而不是生成 IOrderedEnumerable&lt;T&gt;,然后您必须在 List&lt;T&gt; 中重新转换。

    【讨论】:

    • 恕我直言,这是相当不利的,因为我将“有序列表”视为原始数据的视图。所以,我更愿意让原始列表“原封不动”。
    • @FrederikGheysels 幸运的是你可以选择:-)
    【解决方案2】:

    查看 LINQ OrderBy 扩展方法。

    MyList.OrderBy (p => p.Category);
    

    如果您需要更复杂的方式对类别进行排序,您可以创建一个实现 IComparer 接口的类,并在其中实现您的排序逻辑。

            public class SmartphonesFirst : IComparer<Product>
            {
                const string Smartphone = "Smartphone";
    
                public int Compare( Product x, Product y )
                {
                    if( x.Category == Smartphone && y.Category != Smartphone )
                    {
                        return -1;
                    }
                    if( y.Category == Smartphone && x.Category != Smartphone )
                    {
                        return 1;
                    }
                    else
                    {
                        return Comparer<String>.Default.Compare (x.Category, y.Category);
                    }
                }
            }
    

    你可以不使用 LINQ 来做到这一点:

                var l = new List<Product> ();
                l.Add (new Product ()
                {
                    Name = "Omnia 7",
                    Category = "Smartphone"
                });
    
                l.Add (new Product ()
                {
                    Name = "Mercedes",
                    Category = "Car"
                });
    
                l.Add (new Product ()
                {
                    Name = "HTC",
                    Category = "Smartphone"
                });
    
                l.Add (new Product ()
                {
                    Name = "AMD",
                    Category = "CPU"
                });
    
                l.Sort (new SmartphonesFirst ());
    
                foreach( var p in l )
                {
                    Console.WriteLine (String.Format ("{0} : {1}", p.Category, p.Name));
                }
    

    或者,使用 LINQ:

                var l = new List<Product> ();
                l.Add (new Product ()
                {
                    Name = "Omnia 7",
                    Category = "Smartphone"
                });
    
                l.Add (new Product ()
                {
                    Name = "Mercedes",
                    Category = "Car"
                });
    
                l.Add (new Product ()
                {
                    Name = "HTC",
                    Category = "Smartphone"
                });
    
                l.Add (new Product ()
                {
                    Name = "AMD",
                    Category = "CPU"
                });
    
                var sorted = l.OrderBy (p => p, new SmartphonesFirst ());
    
                foreach ( var p in sorted )
                {
                    Console.WriteLine (String.Format ("{0} : {1}", p.Category, p.Name));
                }
    

    【讨论】:

      【解决方案3】:

      您可以使用Sort 方法和自定义比较,按类别(降序)然后按产品(升序)排序:

      MyList.Sort((a, b) => {
        // compare b to a to get descending order
        int result = b.Category.CompareTo(a.Category);
        if (result == 0) {
          // if categories are the same, sort by product
          result = a.Product.CompareTo(b.Product);
        }
        return result;
      });
      

      如果你想挑出智能手机,然后升序排序:

      MyList.Sort((a, b) => {
        int result = (a.Category == "SmartPhone" ? 0 : 1) - (b.Category == "SmartPhone" ? 0 : 1);
        if (result == 0) {
          result = a.Category.CompareTo(b.Category);
          if (result == 0) {
            result = a.Product.CompareTo(b.Product);
          }
        }
        return result;
      });
      

      【讨论】:

      • +1。当类别字段相等时,进一步对产品字段进行排序。
      猜你喜欢
      • 2019-02-08
      • 1970-01-01
      • 1970-01-01
      • 2019-06-28
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多