我目前正在使用列表中包含的对象类型中的'CompareTo'方法对C#列表进行排序。我想通过自己的WBS(工作分解结构)递增的所有项目进行排序,我可以管理这个很好用下面的代码:用两个参数对列表进行排序使用CompareTo
public int CompareTo(DisplayItemsEntity other)
{
string[] instanceWbsArray = this.WBS.Split('.');
string[] otherWbsArray = other.WBS.Split('.');
int result = 0;
for (int i = 0; i < maxLenght; i++)
{
if (instanceWbsArray[i].Equals(otherWbsArray[i]))
{
continue;
}
else
{
result = Int32.Parse(instanceWbsArray[i]).CompareTo(Int32.Parse(otherWbsArray[i]));
break;
}
}
return result;
}
现在,我想能够排序考虑多个参数,就像在项目名称中按字母顺序排列,然后再考虑第二个项目是WBS。我怎样才能做到这一点?
如何使用'OrderBy()'方法? –
@JFB假设你使用的是.NET 3.5或更高版本,你需要在文件的'using'指令中包含:'using System.Linq;' – Mir
oops!现在我很尴尬。谢谢! –