如何在降序排序的DateTime对象的ArrayList? [英] How to sort ArrayList of DateTime objects in descending order?

查看:125
本文介绍了如何在降序排序的DateTime对象的ArrayList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何按降序排列做我排序的ArrayList的DateTime对象?

How do I sort ArrayList of DateTime objects in descending order?

感谢您。

推荐答案

首先,除非你被卡住使用框架1.1,你不应该在所有使用的ArrayList 。您应该使用强类型的通用列表< D​​ateTime的方式> 而不是

First of all, unless you are stuck with using framework 1.1, you should not be using an ArrayList at all. You should use a strongly typed generic List<DateTime> instead.

有关自定义排序存在的超载排序方法,它需要一个比较器。通过扭转经常比较,你会得到降序排序:

For custom sorting there is an overload of the Sort method that takes a comparer. By reversing the regular comparison you get a sort in descending order:

list.Sort(delegate(DateTime x, DateTime y){ return y.CompareTo(x); });



更新:



随着lambda表达式C#3,委托是更容易地创建:

Update:

With lambda expressions in C# 3, the delegate is easier to create:

list.Sort((x, y) => y.CompareTo(x));

这篇关于如何在降序排序的DateTime对象的ArrayList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆