728x90
728x170

문자와 숫자가 섞여 있는 경우 문자와 숫자를 정렬해준다.

원본
a1
a12
a2
b2
b1
b11

결과
a1
a2
a12
b1
b2
b11

public static IEnumerable<string> NaturalSort(IEnumerable<string> list)
{
   
int maxLen = list.Select(s => s.Length).Max();
   
Func<string, char> PaddingChar = s => char.IsDigit(s[0]) ? ' ' : char.MaxValue;

   
return list
           
.Select(s =>
               
new
               
{
                   
OrgStr = s,
                   
SortStr = Regex.Replace(s, @"(\d+)|(\D+)", m => m.Value.PadLeft(maxLen, PaddingChar(m.Value)))
               
})
           
.OrderBy(x => x.SortStr)
           
.Select(x => x.OrgStr);
}
728x90
그리드형
Posted by kjun
,