[英]sort fileinfo list by int within fileinfo name
I have some trouble to sort a List, I need it to be sorted by the FileInfo.Name attribut, within this name is an interger with unknown length at the very beginning of the string. 我在对列表进行排序时遇到了一些麻烦,我需要按FileInfo.Name属性对其进行排序,在此名称内是一个整数,字符串的开头部分的长度未知。 I need to sort the list by this number. 我需要按此数字对列表进行排序。
As my experience it is very difficult to compare two strings by a number within this string, so I could need some help. 根据我的经验,很难通过字符串中的数字比较两个字符串,因此我需要一些帮助。
This is my list: 这是我的清单:
I need the list to be sorted from this [1,13,2,3,4,5] into this [1,2,3,4,5,13] 我需要将列表从[1,13,2,3,4,5]排序为[1,2,3,4,5,13]
Here is what I have tried so far: 到目前为止,这是我尝试过的:
infos.Sort((a, b) => a.Split('-')[0].CompareTo(b.Split('-')[0]));
Of course this can not work as I try to compare strings by numbers.... 当然,这无法工作,因为我尝试通过数字比较字符串。
EDIT: Unfortunaely the solution from Mukund does not work as shown in this image: 编辑:不幸的是,来自Mukund的解决方案不起作用,如该图所示:
You can use this. 您可以使用它。
infos.OrderBy(x => Convert.ToInt32(x.Split('-')[0]))
class Program11
{
static void Main(string [] args)
{
var infos = new List<string> { "1-100.jpg", "13-11.jpg", "2-145.jpg", "3-421.jpg", "4-842.jpg", "5-1000.jpg" };
var orderedList = infos.OrderBy(x => Convert.ToInt32(x.Split('-')[0]));
foreach (var lstItem in orderedList)
{
Console.WriteLine(lstItem);
}
Console.ReadKey();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.