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

时间:2022-05-19 15:07:51

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

如何按降序对DateTime对象的ArrayList进行排序?

Thank you.

5 个解决方案

#1


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.

首先,除非你坚持使用框架1.1,否则根本不应该使用ArrayList。您应该使用强类型通用List

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:

对于自定义排序,Sort方法的重载需要比较器。通过反转常规比较,您可以按降序排序:

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

Update:

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

使用C#3中的lambda表达式,委托更容易创建:

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

#2


As "Guffa" already said, you shouldn't be using ArrayList unless you are in .NET 1.1; here's a simpler List<DateTime> example, though:

正如“Guffa”已经说过的那样,除非你在.NET 1.1中,否则你不应该使用ArrayList;这里是一个更简单的List 示例,但是:

List<DateTime> dates = ... // init and fill
dates.Sort();
dates.Reverse();

Your dates are now sorted in descending order.

您的日期现在按降序排列。

#3


Use a DateTime Comparer that sorts in reverse. Call Sort.

使用反向排序的DateTime Comparer。呼叫排序。

public class ReverseDateComparer:IComparer{ 
    public int  Compare(object x, object y){
        return -1 * DateTime.Compare(x, y);
    }
}

list.Sort(new ReverseDateComparer());

#4


If you are using .NET 3.5:

如果您使用的是.NET 3.5:

// ArrayList dates = ...
var sortedDates = dates.OrderByDescending(x => x);
// test it
foreach(DateTime dateTime in sortedDates)
  Console.WriteLine(dateTime);

#5


List<T>.Sort(YourDateTimeComparer) where YourDateTimeComparer : IComparer<DateTime>

Here is an example of custom IComparer use: How to remove duplicates from int[][]

以下是自定义IComparer使用的示例:如何从int [] []中删除重复项

#1


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.

首先,除非你坚持使用框架1.1,否则根本不应该使用ArrayList。您应该使用强类型通用List

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:

对于自定义排序,Sort方法的重载需要比较器。通过反转常规比较,您可以按降序排序:

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

Update:

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

使用C#3中的lambda表达式,委托更容易创建:

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

#2


As "Guffa" already said, you shouldn't be using ArrayList unless you are in .NET 1.1; here's a simpler List<DateTime> example, though:

正如“Guffa”已经说过的那样,除非你在.NET 1.1中,否则你不应该使用ArrayList;这里是一个更简单的List 示例,但是:

List<DateTime> dates = ... // init and fill
dates.Sort();
dates.Reverse();

Your dates are now sorted in descending order.

您的日期现在按降序排列。

#3


Use a DateTime Comparer that sorts in reverse. Call Sort.

使用反向排序的DateTime Comparer。呼叫排序。

public class ReverseDateComparer:IComparer{ 
    public int  Compare(object x, object y){
        return -1 * DateTime.Compare(x, y);
    }
}

list.Sort(new ReverseDateComparer());

#4


If you are using .NET 3.5:

如果您使用的是.NET 3.5:

// ArrayList dates = ...
var sortedDates = dates.OrderByDescending(x => x);
// test it
foreach(DateTime dateTime in sortedDates)
  Console.WriteLine(dateTime);

#5


List<T>.Sort(YourDateTimeComparer) where YourDateTimeComparer : IComparer<DateTime>

Here is an example of custom IComparer use: How to remove duplicates from int[][]

以下是自定义IComparer使用的示例:如何从int [] []中删除重复项