如何按字段对列表进行排序

发布于 2024-12-11 07:35:35 字数 374 浏览 0 评论 0原文

美好的一天 4 你们

我有一个对象列表

我的对象就像

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

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

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

我将每个对象插入到我的测试中所以它就像

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

现在我需要按类别排序/排序我的列表

因为我需要我的列表首先显示智能手机类别然后显示其他类别

Good day 4 u all

I have a list of objects

My objects like

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

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

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

And I insert each object into my test so its like

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

And now I need to sord/order MyList by the category

As I need my list to show the SmartPhone category first then other

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦里兽 2024-12-18 07:35:35

您可以使用 List.Sort

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

相对于 LINQ OrderBy 的优势在于,您可以就地对列表进行排序,而不是生成一个 IOrderedEnumerable那么你必须在 List 中重新转换。

You can use List.Sort

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

The advantage over the LINQ OrderBy is that you'll order the list in-place instead of generating an IOrderedEnumerable<T> that then you have to re-transform in a List<T>.

死开点丶别碍眼 2024-12-18 07:35:35

查看 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));
            }

Check out the LINQ OrderBy extension method.

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

If you need a more complex way to sort the categories, you could create a class which implements the IComparer interface, and implement your sort logic in it.

        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);
                }
            }
        }

You can do it without using 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));
            }

Or, with using 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));
            }
小苏打饼 2024-12-18 07:35:35

您可以使用 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;
});

You can use the Sort method and a custom comparison, to sort by category (descending) and then by product (ascending):

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;
});

If you want to single out smartphones, and then sort ascending:

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;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文