如何在C#中按特定字段对对象列表进行排序?
我有这门课:如何在C#中按特定字段对对象列表进行排序?,c#,sorting,C#,Sorting,我有这门课: public class StatInfo { public string contact; public DateTime date; public string action; } 然后我有一个StatInfo列表,但我不确定如何根据日期字段对其进行排序。我应该使用排序方法吗?我应该创造我自己的吗 var _allStatInfo = new List<StatInfo>(); // adding lots of stuff in it _allSta
public class StatInfo
{
public string contact;
public DateTime date;
public string action;
}
然后我有一个StatInfo列表,但我不确定如何根据日期字段对其进行排序。我应该使用排序方法吗?我应该创造我自己的吗
var _allStatInfo = new List<StatInfo>();
// adding lots of stuff in it
_allStatInfo.SortByDate???
var\u allStatInfo=new List();
//添加了很多东西
_allStatInfo.SortByDate???
不必编写大量代码(如果可能)的最佳方法是什么
谢谢使用lambda表达式将一对映射到比较:
_allStatInfo.Sort((x, y) => x.date - y.date);
使用LINQ:
var sortedList = _allStatInfo.OrderBy(si => si.date).ToList();
对原始列表进行排序:
_allStatInfo.Sort(new Comparison<StatInfo>((x, y) => DateTime.Compare(x.date, y.date)));
\u allStatInfo.Sort(新比较((x,y)=>DateTime.Compare(x.date,y.date));
对于日期时间,不需要进行比较
_allStatInfo.OrderyBy(d => d.date);
或
为了说明Robert C.Cartaino的答案:
public class StatInfo : IComparable<StatInfo>
{
public string contact;
public DateTime date;
public string action;
public int CompareTo(StatInfo value)
{
return this.date.CompareTo(value.date);
}
}
var _allStatInfo = new List<StatInfo>();
// this now sorts by date
_allStatInfo.Sort();
public类StatInfo:i可比较
{
公众联络;
公共日期时间日期;
公共字符串操作;
公共整数比较(StatInfo值)
{
返回此.date.CompareTo(value.date);
}
}
var_allStatInfo=新列表();
//现在按日期排序
_allStatInfo.Sort();
这不是最普遍的解决方案,但如果你只想按日期排序,那就很好了。而且,正如Robert所说,您仍然可以通过将IComparer参数传递给sort方法来覆盖默认排序。我知道您已经得到了答案,但是
Comparison<StatInfo> comparison = (x, y) => DateTime.Compare(x.date, y.date);
_allStatInfo.Sort(comparison);
ProjectOnComparer
类创建一个IComparer
实现-
它是的一部分,我已经在的底部包含了一个未注释的版本
这就是答案。然后你会写:
_allStatInfo.Sort(ProjectionComparer<StatInfo>.Create(x => x.date));
它对我有用
等Jon Skeet出现。他的《深入C#3:掌握C#2和C#3所需要的东西》中有一个很大的章节就是关于这一点的。太晚了!当我关于C#的一个问题没有得到Jon Skeet的回答时,我总是感到惊讶:)标题是错的。说“array”谢谢你的回答,但我得到一个错误:无法将lambda表达式转换为System.Collections.Generic.IComparer类型因为它不是委托类型谢谢,第二个方法起作用了。我不使用LINQ,因此无法测试第二个。这两个方法都返回列表,因此您需要_allStatInfo=_allStatInfo.OrderBy(d=>d.date.ToList();谢谢,但是如果我没有弄错的话,列表没有这样的功能。我想这是LINQ。我没有使用LINQOh谢谢,就在我说我没有从你那里得到关于C#问题的答案时:)我会接受Ben的答案,但我对你的答案投了更高的票,我相信这是正确的below@Ben:如果可以的话,我只是不喜欢那么多括号:)Jon,在你的
Compare
方法中,真的需要空检查吗?那个应该留给打电话的人。@nawfal:我不这么认为。您希望能够通过foo=>foo.Name
订购,而不必执行foo=>foo==null?null:foo.Name
IMO.@JonSkeet您提供的示例可以写得更好,但仅此而已。如果我们提供一个像Compute(int,string)
这样的方法作为投影,像\u allStatInfo.Sort(projectoncomparer.Create(x=>Compute(someInt,x))
这样的方法,其中x
的空值是完全可以接受的?如果必须是完全通用的,则引发空引用异常或忽略空值应由投影决定。谢谢。它并不真正适用于我的情况,因为在这种特殊情况下,我只会按日期排序,但我会记住这一点later@marcgg:我答案中的代码使按日期排序成为默认行为。所以,如果你只是按日期排序,我想这正是你想要的。
Comparison<StatInfo> comparison = (x, y) => x.date.CompareTo(y.date);
_allStatInfo.Sort(comparison);
_allStatInfo.Sort(ProjectionComparer<StatInfo>.Create(x => x.date));
public static class ProjectionComparer
{
public static ProjectionComparer<TSource, TKey> Create<TSource, TKey>
(Func<TSource, TKey> projection)
{
return new ProjectionComparer<TSource, TKey>(projection);
}
public static ProjectionComparer<TSource, TKey> Create<TSource, TKey>
(TSource ignored, Func<TSource, TKey> projection)
{
return new ProjectionComparer<TSource, TKey>(projection);
}
}
public static class ProjectionComparer<TSource>
{
public static ProjectionComparer<TSource, TKey> Create<TKey>
(Func<TSource, TKey> projection)
{
return new ProjectionComparer<TSource, TKey>(projection);
}
}
public class ProjectionComparer<TSource, TKey> : IComparer<TSource>
{
readonly Func<TSource, TKey> projection;
readonly IComparer<TKey> comparer;
public ProjectionComparer(Func<TSource, TKey> projection)
: this (projection, null)
{
}
public ProjectionComparer(Func<TSource, TKey> projection,
IComparer<TKey> comparer)
{
projection.ThrowIfNull("projection");
this.comparer = comparer ?? Comparer<TKey>.Default;
this.projection = projection;
}
public int Compare(TSource x, TSource y)
{
// Don't want to project from nullity
if (x==null && y==null)
{
return 0;
}
if (x==null)
{
return -1;
}
if (y==null)
{
return 1;
}
return comparer.Compare(projection(x), projection(y));
}
}
// sort array by name
Array.Sort(users, delegate(User user1, User user2)
{
return user1.Name.CompareTo(user2.Name);
});
// write array (output: Betty23 Lisa25 Susan20)
foreach (User user in users) Console.Write(user.Name + user.Age + " ");