如何在c#中按日期时间对集合进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1618863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to sort a collection by datetime in c#
提问by user195968
I have a List that I need to sort by DateTime, the class MyStuff looks like:
我有一个需要按日期时间排序的列表,MyStuff 类看起来像:
public class MyStuff
{
public int Type {get;set;}
public int Key {get;set;}
public DateTime Created {get;set;}
}
I need to be able to sort the collection List by the Created (DateTime) field.
我需要能够通过 Created (DateTime) 字段对集合列表进行排序。
回答by popester
var query =
from m in mystuffcollection
orderby m.Created ascending
select m;
回答by Noldorin
You seem to be working with a List<T>
object, in which case the most efficient (and a simple) method would be the following:
您似乎正在处理一个List<T>
对象,在这种情况下,最有效(也是最简单)的方法如下:
myList.Sort((x, y) => DateTime.Compare(x.Created, y.Created));
This uses the overload of the List.Sortmethod than takes a Comparison<T>
delegate (and thus lambda expression).
这使用List.Sort方法的重载而不是接受Comparison<T>
委托(因此是 lambda 表达式)。
You can of course use the LINQ OrderBy
extension method, but I don't think this offers any advantages, and can be significantly slower, depending on your situation.
您当然可以使用 LINQOrderBy
扩展方法,但我认为这不会提供任何优势,而且速度可能会明显变慢,具体取决于您的情况。
myList = myList.OrderBy(x => x.Created).ToList();
回答by Darleison Rodrigues
You can use using System.Linq;
from entity framework, considering you are using .net core in your application. The Linq provides you both FilterBy and OrderBy methods, for custom FilterBy and OrderBy you need to override the methods in your class or Repository.
using System.Linq;
考虑到您在应用程序中使用 .net 核心,您可以使用实体框架。Linq 为您提供了 FilterBy 和 OrderBy 方法,对于自定义 FilterBy 和 OrderBy,您需要覆盖您的类或存储库中的方法。
override
protected IQueryable<T> FilterBy(IQueryable<T> myProcess, string filterBy, string filterValue)
{
var listFilter = filterBy.Split(',');
var listValues = filterValue.Split(',');
for (int index = 0; index < listFilter.Length; index++)
{
var filter = listFilter[index];
var value = listValues[index];
switch (filter)
{
case "type":
myProcess = myProcess.Where(c => c.Status.Nome.Contains(value));
break;
case "created":
myProcess = myProcess.Where(c => c.Created - DateTime.Parse(value) >= new TimeSpan(0, 0, 0));
default:
break;
}
}
}
override
protected IQueryable<T> OrderBy(IQueryable<T> myProcess, string attribute, string direction)
{
switch (attribute)
{
case "type":
myProcess = (direction == "asc")
? myProcess.OrderBy(c => c.Type)
: myProcess.OrderByDescending(c => c.Type);
return myProcess;
case "created":
myProcess = (direction == "asc")
? myProcess.OrderBy(c => c.Created)
: myProcess.OrderByDescending(c => c.Created);
return myProcess;
}
}
回答by FreakyAli
For those who are looking for a way to sort the data on the basis of a nested property can use the sort function something like below:
对于那些正在寻找基于嵌套属性对数据进行排序的方法的人,可以使用如下所示的 sort 函数:
MyList.Sort((x, y) => x.Datetime.CompareTo(y.Datetime));
Now the major differences between using OrderBy and Sort are performance and return value.
现在使用 OrderBy 和 Sort 之间的主要区别是性能和返回值。
Sort basically sorts the existing List<T>
whereas OrderBy returns a new IEnumerable<T>
Sort 基本上对现有的进行排序,List<T>
而 OrderBy 返回一个新的IEnumerable<T>