Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试根据字符串值对列表排序_C#_List_Sorting - Fatal编程技术网

C# 尝试根据字符串值对列表排序

C# 尝试根据字符串值对列表排序,c#,list,sorting,C#,List,Sorting,因此,age,是一个字符串,可以是类似“0”或“14”的东西,您可以看到图片。我想按年龄对列表进行排序,所以我在OrderBy中将其转换为int。代码编译并运行时没有错误,但列表未排序 我做错了什么?您没有使用结果OrderBy返回已排序的列表 gridMessages.OrderBy(x => Convert.ToInt32(x.age)); 如果gridMessages是一个列表,您也可以对其进行适当排序,而不是创建一个新列表: gridMessages = gridMessages

因此,
age
,是一个字符串,可以是类似“0”或“14”的东西,您可以看到图片。我想按年龄对列表进行排序,所以我在
OrderBy
中将其转换为int。代码编译并运行时没有错误,但列表未排序


我做错了什么?

您没有使用结果
OrderBy
返回已排序的列表

gridMessages.OrderBy(x => Convert.ToInt32(x.age));

如果gridMessages是一个
列表
,您也可以对其进行适当排序,而不是创建一个新列表:

gridMessages = gridMessages.OrderBy(x => Convert.ToInt32(x.age));

一点也不,一个很常见的问题
gridMessages.Sort( (a,b) => a.Age.CompareTo(b.Age));