按元素文件名编号对List <string>中的文件进行排序

时间:2017-04-11 04:27:37

标签: c# string list sorting

我在List<string>

中有以下文件
backup-codes (7).txt
backup-codes.txt
backup-codes (1).txt
backup-codes (2).txt
backup-codes (3).txt
backup-codes (8).txt
backup-codes (6).txt

如何按号码(eg. (#) )对它们进行排序,以便具有最高值的文件始终首先出现在列表中?

1 个答案:

答案 0 :(得分:2)

您可以执行类似

的操作
lstFiles = lstFiles.OrderByDescending(x=> int.Parse(Regex.Replace(x,"[^0-9]+","0"))).ToList<string>();

这假设除了排序之外,文件名中没有其他数字。

这是小提琴手:https://dotnetfiddle.net/Nb38fJ