这个CustomSort可以提供帮助
List<string> list = new List<string>()
{
"input (1).jpg","input (101).jpg", "input (11).jpg", "input (2).jpg"
};
var sortedList = list.CustomSort().ToList();
输出将是:
input (1).jpg
input (2).jpg
input (11).jpg
input (101).jpg
我将这里的代码复制
public static class MyExtensions
{
public static IEnumerable<string> CustomSort(this IEnumerable<string> list)
{
int maxLen = list.Select(s => s.Length).Max();
return list.Select(s => new
{
OrgStr = s,
SortStr = Regex.Replace(s, @"(\d+)|(\D+)", m => m.Value.PadLeft(maxLen, char.IsDigit(m.Value[0]) ? ' ' : '\xffff'))
})
.OrderBy(x => x.SortStr)
.Select(x => x.OrgStr);
}
}