按多个字段对列表(C#)进行排序?

发布于 2024-09-01 00:59:00 字数 388 浏览 4 评论 0原文

我想按多个字段(而不仅仅是一个字段)对 C# 中的对象列表进行排序。例如,假设我有一个名为 X 的类,它有两个属性 A 和 B,并且按顺序有以下对象:

object1 => A =“a”,B =“h”
对象2 => A =“a”,B =“c”
对象3 => A =“b”,B =“x”
对象4 => A =“b”,B =“b”

,我想首先按 A 属性对列表进行排序,当它们相等时,按 B 元素排序,因此顺序为:

“a”“c”
“一个”“h”
“b”“b”
"b" "x"

据我所知,OrderBy 方法按一个参数进行排序。

问题:如何按多个字段对 C# 列表进行排序?

I want to order a List of objects in C# by many fields, not just by one. For example, let's suppose I have a class called X with two Attributes, A and B, and I have the following objects, in that order:

object1 => A = "a", B = "h"
object2 => A = "a", B = "c"
object3 => A = "b", B = "x"
object4 => A = "b", B = "b"

and I want to order the list by A attribute first, and when they are equals, by B element, so the order would be:

"a" "c"
"a" "h"
"b" "b"
"b" "x"

As far as I know, the OrderBy method order by one parameter.

Question: How can I order a C# List by more than one field?

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

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

发布评论

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

评论(4

我也只是我 2024-09-08 00:59:00

使用 ThenBy

var orderedCustomers = Customer.OrderBy(c => c.LastName).ThenBy(c => c.FirstName)

请参阅 MSDN:http://msdn。 microsoft.com/en-us/library/bb549422.aspx

Use ThenBy:

var orderedCustomers = Customer.OrderBy(c => c.LastName).ThenBy(c => c.FirstName)

See MSDN: http://msdn.microsoft.com/en-us/library/bb549422.aspx

最初的梦 2024-09-08 00:59:00

是的,您可以通过指定比较方法来做到这一点。优点是排序的对象不必是 IComparable

   aListOfObjects.Sort((x, y) =>
   {
       int result = x.A.CompareTo(y.A);
       return result != 0 ? result : x.B.CompareTo(y.B);
   });

Yes, you can do it by specifying the comparison method. The advantage is the sorted object don't have to be IComparable

   aListOfObjects.Sort((x, y) =>
   {
       int result = x.A.CompareTo(y.A);
       return result != 0 ? result : x.B.CompareTo(y.B);
   });
花期渐远 2024-09-08 00:59:00

让您的对象类似于

public class MyObject : IComparable
{
    public string a;
    public string b;

    virtual public int CompareTo(object obj)
    {
        if (obj is MyObject)
        {
            var compareObj = (MyObject)obj;
            if (this.a.CompareTo(compareObj.a) == 0)
            {
                // compare second value
                return this.b.CompareTo(compareObj.b);
            }
            return this.a.CompareTo(compareObj.b);
        }
        else
        {
            throw new ArgumentException("Object is not a MyObject ");
        }
    }
}

还请注意 CompareTo 的返回:

http: //msdn.microsoft.com/en-us/library/system.icomparable.compareto.aspx

然后,如果您有 MyObject 列表,请调用 .Sort() 即

var objList = new List<MyObject>();
objList.Sort();

Make your object something like

public class MyObject : IComparable
{
    public string a;
    public string b;

    virtual public int CompareTo(object obj)
    {
        if (obj is MyObject)
        {
            var compareObj = (MyObject)obj;
            if (this.a.CompareTo(compareObj.a) == 0)
            {
                // compare second value
                return this.b.CompareTo(compareObj.b);
            }
            return this.a.CompareTo(compareObj.b);
        }
        else
        {
            throw new ArgumentException("Object is not a MyObject ");
        }
    }
}

also note that the returns for CompareTo :

http://msdn.microsoft.com/en-us/library/system.icomparable.compareto.aspx

Then, if you have a List of MyObject, call .Sort() ie

var objList = new List<MyObject>();
objList.Sort();
清晰传感 2024-09-08 00:59:00

您的对象应实现 IComparable 接口。

有了它,您的类就变成了一个名为 CompareTo(T other) 的新函数。在此函数中,您可以在当前对象和另一个对象之间进行任何比较,并返回一个整数值,了解第一个对象是否大于、小于或等于第二个对象。

Your object should implement the IComparable interface.

With it your class becomes a new function called CompareTo(T other). Within this function you can make any comparison between the current and the other object and return an integer value about if the first is greater, smaller or equal to the second one.

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