[英]Sort Interface list C#
I am trying to sort a Interface list by date but i cant figure it out. 我正在尝试按日期对接口列表进行排序,但我无法弄清楚。
var agencyList = new List<INewsItemBE>();
var item = new News1BE();
//add items
agencyList.Add(item)
var item = new News2BE();
//add items
agencyList.Add(item);
After the items is added I want to sort the agencyList
by date with something like this but I cant get it to work. 添加项目后,我想按日期用类似这样的方式对
agencyList
进行排序,但是我无法使其正常工作。
agencyList.Sort(delegate (News1BE c1, News1BE c2) { return c1.PublishDate.CompareTo(c2.PublishDate); });
agencyList.Sort(delegate (News1BE a, News1BE b) => a.PublishDate.CompareTo(b.PublishDate));
agencyList = agencyList.OrderBy(x => cant find anything at x.).ToList();
Try LINQ: 尝试LINQ:
using System.Linq;
var ordered = agencyList.OrderBy(x => x.PublishDate);
Or if you need it descending: 或者,如果您需要降序排列:
var orderedDesc = agencyList.OrderByDescending(x => x.PublishDate);
if you need the list in the original variable again, you can call .ToList()
: 如果您再次需要原始变量中的列表,则可以调用
.ToList()
:
agencyList = agencyList.OrderBy(x => x.PublishDate).ToList();
Of if LINQ is not working for you (or you wish the best performance and thus decline the overhead brought by LINQ), you can use the .Sort
method you were trying: 如果LINQ不适用于您(或者您希望获得最佳性能,从而减少LINQ带来的开销),则可以使用尝试的
.Sort
方法:
agencyList.Sort((item1, item2) => item1.PublishDate.CompareTo(item2.PublishDate));
You can try SortedList instead of List, so it will be automatically sorted when you add items. 您可以尝试使用SortedList而不是List,这样在添加项目时它将自动进行排序。
Define your classes/interfaces: 定义您的类/接口:
interface INews1BE
{
DateTime PublishedDate { get; set; }
}
class News1BE : INews1BE
{
public DateTime PublishedDate { get; set; }
}
Define comparer for DateTime: 为DateTime定义比较器:
class DateTimeAscendingComparer : IComparer<DateTime>
{
public int Compare(DateTime a, DateTime b)
{
return a.CompareTo(b);
}
}
Now create comparer and sorted list passing comparer to the SortedList constructor: 现在创建比较器和将比较器传递给SortedList构造函数的排序列表:
var comparer = new DateTimeAscendingComparer();
var sortedList = new SortedList<DateTime, INews1BE>(comparer);
Add some items and check the results: 添加一些项目并检查结果:
var item1 = new News1BE()
{
PublishedDate = DateTime.Now
};
var item2 = new News1BE()
{
PublishedDate = item1.PublishedDate.AddSeconds(-30)
};
sortedList.Add(item1.PublishedDate, item1);
//item2 goes before item1
sortedList.Add(item2.PublishedDate, item2);
foreach(var x in sortedList)
{
Console.WriteLine(x.Key);
}
You can also access your object through Value: 您还可以通过值访问对象:
foreach (var x in sortedList)
{
Console.WriteLine(x.Value.PublishedDate);
}
Or you can iterate over Values to get immediate access to your INews1BE object: 或者,您可以遍历Values以立即访问您的INews1BE对象:
foreach(var x in sortedList.Values)
{
Console.WriteLine(x.PublishedDate);
}
Actually you don't have to define comparer for DateTime if you just care about ascending order - this is the default behavior, so you can just create SortedList without passing comparer to the constructor: 实际上,如果您只关心升序,则不必为DateTime定义比较器-这是默认行为,因此您可以创建SortedList而不将比较器传递给构造函数:
var sortedList = new SortedList<DateTime, INews1BE>();
But if you need Descending order just switch DateTime a
with DateTime b
when comparing: 但是,如果需要降序,只需在比较时将DateTime
a
与DateTime b
切换即可:
class DateTimeDescendingComparer : IComparer<DateTime>
{
public int Compare(DateTime a, DateTime b)
{
return b.CompareTo(a);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.