我有一个 C# 自定义对象列表,我需要按两个不同的变量对其进行排序,一个是 bool 值,另一个是字符串。我可以按其中一个 条件进行排序,但我无法弄清楚如何组合它们。排序应该首先是所有 bool 值 (CheckedIn),然后是每个值的姓氏。现在我用
result.Sort((x, y) => string.Compare(x.CheckedIn.ToString(), y.CheckedIn.ToString()));
result.Sort((x, y) => string.Compare(x.LastName, y.LastName));
但是我怎样才能组合起来让我的结果像
CheckedIn-Name
No - Aames
No - Smith
Yes - Barnes
Yes - Peters
最佳答案
使用 linq。
如果你有类对象的列表 L
public class temp
{
public bool x;
public string y;
}
然后使用:
L.orderby(a=>a.x).thenby(a=>a.y);
您可以根据需要将其链接起来。
关于C# 列表按两列排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7099741/