C# 列表>> 具有 3 个属性的订单 .Net 2.0

发布于 2024-07-24 08:12:21 字数 299 浏览 6 评论 0原文

假设我有一个包含 Name、Age、Level 属性的 Person 类。

我知道如何按其中一项属性进行排序,

        PersonList.Sort(delegate(Person p1, Person p2) {
            return p1.Name.CompareTo(p2.Name);
        });

但是我如何按姓名、年龄和级别进行排序。

sql语句的等价物:ORDER BY Name, Age, Level

谢谢

Say I have a Person class with Name, Age, Level properties.

I know how to order by one of the properties, with

        PersonList.Sort(delegate(Person p1, Person p2) {
            return p1.Name.CompareTo(p2.Name);
        });

But how can I order by Name, Age and Level.

An equivalente of the sql sentence : ORDER BY Name, Age, Level

Thank you

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

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

发布评论

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

评论(8

世界等同你 2024-07-31 08:12:21

调整您当前的代码:

PersonList.Sort(delegate(Person p1, Person p2) {
        int r = p1.Name.CompareTo(p2.Name);
        if (r == 0) r = p1.Age.CompareTo(p2.Age);
        if (r == 0) r = p1.Level.CompareTo(p2.Level);
        return r;
    });

或者,一个简单的 linq-ish 解决方案:

PersonList = PersonList.OrderBy(p => p.Name)
                       .ThenBy(p => p.Age)
                       .ThenBy(p => p.Level).ToList();

Adapting your current code:

PersonList.Sort(delegate(Person p1, Person p2) {
        int r = p1.Name.CompareTo(p2.Name);
        if (r == 0) r = p1.Age.CompareTo(p2.Age);
        if (r == 0) r = p1.Level.CompareTo(p2.Level);
        return r;
    });

or, a simple linq-ish solution:

PersonList = PersonList.OrderBy(p => p.Name)
                       .ThenBy(p => p.Age)
                       .ThenBy(p => p.Level).ToList();
后eg是否自 2024-07-31 08:12:21

您是否考虑过切换到 .NET 3.5 并使用 LINQ? 在 LINQ 中,这样的事情非常简单:

personList = personList.OrderBy(p => p.Name).
    ThenBy(p => p.Age).ThenBy(p => p.Level).ToList();

Have you considered switching to .NET 3.5 and using LINQ? Things like this are really easy in LINQ:

personList = personList.OrderBy(p => p.Name).
    ThenBy(p => p.Age).ThenBy(p => p.Level).ToList();
小女人ら 2024-07-31 08:12:21

我会在您的 Person 类上实现 IComparable 。 在自定义比较方法中,您可以设置对所有三个属性进行比较的逻辑,并确保这三个属性具有适当的“权重”(例如,如果 Person 的两个实例具有相同的名称,您应该按 Age 或 Level 进行下一步排序)。

I would implment IComparable on your Person class. In your custom compare method, you can set the logic to compare on all three properties, and make sure the three have the proper "weight" (e.g., if two instances of Person have the same name, should you sort next on Age or Level).

指尖凝香 2024-07-31 08:12:21

假设您使用的是 .NET 3.5

 IQueryable<Person> people = PersonList.AsQueryable();

 return people.OrderBy(x => x.Name).ThenBy(x => x.Age).ThenBy(x => x.level);

Assuming you are using .NET 3.5

 IQueryable<Person> people = PersonList.AsQueryable();

 return people.OrderBy(x => x.Name).ThenBy(x => x.Age).ThenBy(x => x.level);
清风夜微凉 2024-07-31 08:12:21

如果您有可用的 C# 3.0(LINQ 支持),那么您可以执行以下操作:

var result = (from p in PersonList
              orderby p.Name, p.Age, p.Level
              select p).ToList();

If you have C# 3.0 available (LINQ support), then you can do the following:

var result = (from p in PersonList
              orderby p.Name, p.Age, p.Level
              select p).ToList();
迷爱 2024-07-31 08:12:21

您考虑过 LINQ to Objects 吗?

var x = (from p in PersonList
        orderby p.Name, p.Age, p.Level
        select p).ToList();

Have you considered LINQ to Objects?

var x = (from p in PersonList
        orderby p.Name, p.Age, p.Level
        select p).ToList();
短叹 2024-07-31 08:12:21

您需要对要排序的第一个属性调用 CompareTo,获取返回的值,如果为 0,则对下一个属性调用 CompareTo,并重复,每次返回 0 时,继续处理下一个属性直到获得非零值(然后返回该值)或者到达要排序的属性的末尾。

You need to call CompareTo on the first property you want to order by, get the value that is returned, and if it is 0, then call CompareTo on the next property, and repeat, each time 0 is returned, continue onto the next property until you get a non-zero value (which you then return) or you get to the end of the properties you want to sort by.

非要怀念 2024-07-31 08:12:21
var result = from p in persons
             orderby p.Name, p.Age, p.Level
             select p;
var result = from p in persons
             orderby p.Name, p.Age, p.Level
             select p;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文