【发布时间】:2012-12-13 12:04:37
【问题描述】:
我的问题是关于IComparer 接口,我以前从未使用过它,所以希望您能帮助我正确设置所有内容。
我必须使用接口按照另一个List<int> 的确切顺序对自己的对象列表进行排序。
我在网上找不到任何对这个问题有用的东西,我发现的都是 linq 语句,我不能使用。
示例代码如下:
public class What : IComparer<What>
{
public int ID { get; set; }
public string Ever { get; set; }
public What(int x_ID, string x_Ever)
{
ID = x_ID;
Ever = x_Ever;
}
public int Compare(What x, What y)
{
return x.ID.CompareTo(y.ID);
}
}
需要处理的一些数据:
List<What> WhatList = new List<What>()
{
new What(4, "there"),
new What(7, "are"),
new What(2, "doing"),
new What(12, "you"),
new What(78, "Hey"),
new What(63, "?")
};
以及顺序正确的列表:
List<int> OrderByList = new List<int>() { 78, 4, 63, 7, 12, 2 };
那么现在我如何告诉IComparer 按OrderByList 排序?
我真的不知道该怎么做,我知道使用 linq 会很容易,但我没有机会使用它。
【问题讨论】:
-
出于兴趣,为什么没有 LINQ?
-
是否可以假设所有
ID值都存在于您的OrderBylist集中?
标签: c# .net list interface icomparer