[英]List ordered in a particular way
public class Program
{
static void Main(string[] args)
{
List<Category> categoryList = new List<Category>()
{
new Category(){CategoryType = "BP"},
new Category(){ CategoryType ="BY"},
new Category(){ CategoryType ="BT1"},
new Category(){ CategoryType ="BT3"},
new Category(){ CategoryType ="BTU"},
new Category(){ CategoryType ="BT2"},
};
categoryList = categoryList.OrderBy(x => x.CategoryType).ToList();
categoryList.ForEach(item => Console.WriteLine(item.CategoryType));
Console.ReadKey();
}
public class Category
{
public string CategoryType { get; set; }
public int Count { get; set; }
}
}
Through above program I am getting result like P 通过上面的程序,我得到像P的结果
BT1
BT2
BT3
BTU
BY
However I want to have the result as BP, BY, BT1, BT2, BT3,BTU
. 但是我想得到的结果为
BP, BY, BT1, BT2, BT3,BTU
。
You can order by the category's name's length, and then by its value: 您可以按类别名称的长度,然后按其值排序:
categoryList =
categoryList.OrderBy(x => x.CategoryType.Length).ThenBy(x => x.CategoryType).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.