ThenBy() 메서드를 사용하여 순서대로 요소를 정렬합니다.
다음과 같은 문자열 배열이 있습니다.
string[] str = { "AAA", "AAAA", "A", "AAAAA", "AAAAAAAAA" }; 이제 람다 표현식을 사용하고 ThenBy() 메서드 내부에 조건을 설정하여 문자열이 가진 문자 수에 따라 문자열을 정렬합니다.
IEnumerable<string> res = str.AsQueryable() .OrderBy(alp => alp.Length).ThenBy(alp => alp);
예
using System;
using System.Linq;
using System.Collections.Generic;
public class Demo {
public static void Main() {
string[] str = { "AAA", "AAAA", "A", "AAAAA", "AAAAAAAAA" };
IEnumerable<string> res = str.AsQueryable() .OrderBy(alp => alp.Length).ThenBy(alp => alp);
foreach (string arr in res)
Console.WriteLine(arr);
}
} 출력
A AAA AAAA AAAAA AAAAAAAAA