对象排序问题

发布于 2024-12-04 04:45:00 字数 640 浏览 1 评论 0原文

我有一个 Car 集合:

var cars = new List<Car>();
cars.Add(new Car { ProductionDate = new DateTime(2011,02,02) });
cars.Add(new Car { ProductionDate = new DateTime(2011, 01, 01) });
cars.Add(new Car { ProductionDate = new DateTime(2011,04,04,04,04,04) });
cars.Add(new Car { ProductionDate = new DateTime(2011, 03, 03, 3, 3, 3) });

我需要按 ProductionDate 对它进行排序。

结果应该是,首先我将拥有带有日期和时间的汽车,因此应该是生产日期为 2011, 03, 03, 3, 3, 3 的汽车,最后应该是生产日期为 2011, 02, 02 的汽车生产日期。第二个应该是 2011, 04, 04, 04, 04, 04 的汽车,第三个应该是 2011, 02, 02 的汽车。

我可以使用 foreach 来做到这一点,但我相信有更好的方法做吧。

I have a collection of Car:

var cars = new List<Car>();
cars.Add(new Car { ProductionDate = new DateTime(2011,02,02) });
cars.Add(new Car { ProductionDate = new DateTime(2011, 01, 01) });
cars.Add(new Car { ProductionDate = new DateTime(2011,04,04,04,04,04) });
cars.Add(new Car { ProductionDate = new DateTime(2011, 03, 03, 3, 3, 3) });

I need to sort it by ProductionDate.

Result should be that at first I will have cars with date with time, so it should be car with 2011, 03, 03, 3, 3, 3 as production date, and at the end should be car with 2011, 02, 02 as production date. Second should be car with 2011, 04, 04, 04, 04, 04, and the third with 2011, 02, 02.

I can do it by using foreach but I believe that there is nicer way to do it.

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

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

发布评论

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

评论(3

如何视而不见 2024-12-11 04:45:01
TimeSpan zeroTime = new TimeSpan(0);
var sortedCars = cars.OrderBy(c => c.ProductionDate.TimeOfDay.Equals(zeroTime) ? 1 : 0)
                     .ThenBy(c => c.ProductionDate)
                     .ToList();
TimeSpan zeroTime = new TimeSpan(0);
var sortedCars = cars.OrderBy(c => c.ProductionDate.TimeOfDay.Equals(zeroTime) ? 1 : 0)
                     .ThenBy(c => c.ProductionDate)
                     .ToList();
月野兔 2024-12-11 04:45:01

我有类似的东西

    var carsWithoutProductionTime = from car in cars
                                    where car.ProductionDate.Hour == 0
                                    orderby car.ProductionDate
                                    select car;

    var carsWithProductionTime = from car in cars
                                 where car.ProductionDate.Hour != 0
                                 orderby car.ProductionDate
                                 select car;

    var mergedCars = carsWithProductionTime.Union(carsWithoutProductionTime);

,但看起来很丑。我希望看到更复杂的东西:)

I have something like that

    var carsWithoutProductionTime = from car in cars
                                    where car.ProductionDate.Hour == 0
                                    orderby car.ProductionDate
                                    select car;

    var carsWithProductionTime = from car in cars
                                 where car.ProductionDate.Hour != 0
                                 orderby car.ProductionDate
                                 select car;

    var mergedCars = carsWithProductionTime.Union(carsWithoutProductionTime);

but It looks ugly. I would like to see something more sophisticated :)

向日葵 2024-12-11 04:45:00
cars.Sort((p, q) => {
    var tm1 = p.ProductionDate.TimeOfDay;
    var tm2 = q.ProductionDate.TimeOfDay;

    if (tm1.Ticks == 0) {
        if (tm2.Ticks == 0) {
            return p.ProductionDate.CompareTo(q.ProductionDate);
        }
        return 1;
    } else if (tm2.Ticks == 0) {
        return -1;
    } else {
        return p.ProductionDate.CompareTo(q.ProductionDate);
    }
});

但请记住:如果汽车在 0:00 制造出来会发生什么?日期时间由数据+时间组成。你无法看到时间部分是否缺失!

我要补充的是,如果您需要使用 Enumerable.OrderBy,那么您可以将我的 lamdba 函数与您可以在互联网上找到的 LambdaComparer 一起使用(如建议的那样) sll)

cars.Sort((p, q) => {
    var tm1 = p.ProductionDate.TimeOfDay;
    var tm2 = q.ProductionDate.TimeOfDay;

    if (tm1.Ticks == 0) {
        if (tm2.Ticks == 0) {
            return p.ProductionDate.CompareTo(q.ProductionDate);
        }
        return 1;
    } else if (tm2.Ticks == 0) {
        return -1;
    } else {
        return p.ProductionDate.CompareTo(q.ProductionDate);
    }
});

But remember: what happens if a car is built at 0:00? A DateTime is made of a Data+Time. You can't see if the Time part is missing!

I'll add that if you need to use the Enumerable.OrderBy, then you can use my lamdba function with the LambdaComparer that you can find around the internet (as suggested by sll)

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