c# - 删除 list<string> 中的所有字母并对其进行排序

标签 c# linq

我今天遇到了这个问题,我有一个类似的列表

List<string> listVoie =new List<string>(){"V2","V1","3","V4","5","15"};

我必须有序地使用这些值;所以我需要对它进行排序;但问题是某些值包含字母;换句话说;那些字母对我没用,我需要删除它们,现在,我试过了

 List<string> listFilterVoie = listVoie.Where(char.IsDigit).ToList();

摆脱那些字母表;但似乎效果不佳;我该如何对这个列表进行排序,谢谢您的回答;

最佳答案

这应该有效:

listVoie = listVoie
        .Select(str => new string(str.Where(Char.IsDigit).ToArray()))
        .Where(str => str.Length > 0)
        .OrderBy(str => int.Parse(str))
        .ToList();

它从字符串中删除所有不是数字的字符,按 int 排序-value 并创建一个新的 List<string> .

关于c# - 删除 list<string> 中的所有字母并对其进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22382853/

相关文章:

c#输出可能的路线

c# - 从 lambda 表达式创建字符串

c# - 包需要 NuGet 客户端版本 '2.12'

c# - 如何解决 Cannot consume scoped service

c# - 如何在 ASP.NET Web API 中对后台任务进行排队

c# - Linq 和 ListView 以及包含

c# - 将字符数组转换为枚举数组?

linq - 为 linq groupby 编写自定义比较器

c# - 如何在 Linq 查询中获取分组结果的平均值

c# - Web Api IEnumerable 顺序