c# 对自定义列表进行排序

标签 c# linq list sorting object

我有以下列表:

public class Address{
    public string Number { get; set; }
    public string Street { get; set; }
    public string Suburb { get; set; }
}

List<Address> MyAddressList = new List<Address>();

我想做的是按 Suburb 然后是 Street 对列表进行排序。 我已经看到我可以按属性之一排序(在本例中为郊区):

MyAddressList = MyAddressList.OrderBy( x => x.Suburb ).ToList();

但我想先按郊区再按街道排序。 谢谢

最佳答案

您可以使用 ThenBy(或 ThenByDescending)调用链接进一步排序:

MyAddressList = MyAddressList.OrderBy( x => x.Suburb ).ThenBy(x => x.Street).ToList();

关于c# 对自定义列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15754973/

相关文章:

html - 在 Polymer.dart 中使用列表

c# - Active Directory - 检查用户名/密码

c# - 在 C# 中使用 .Show() 方法后,由于循环而导致对话框无法正确显示

c# - 在 WPF 项目中包含 DLL 作为嵌入式资源

c# - 如果表中没有记录,如何返回false

ruby - 林克 map !或收集!

c# - SystemEvents.TimeChanged 触发两次

c# - 如何通过加入获得最大日期的记录?

Python的列表理解和其他更好的实践

python - 如何从Python的范围中排除列表?