orderby 단어를 사용하여 목록을 오름차순 또는 내림차순으로 정렬합니다.
다음은 목록입니다 -
List<string> myList = new List<string>();
myList.Add("truck");
myList.Add("bus");
myList.Add("bicycle");
myList.Add("motorbike"); 이제 목록을 내림차순으로 정렬해 보겠습니다 -
myLen = from element in myList orderby element.Length descending select element;
다음은 전체 코드입니다 -
예
using System;
using System.Collections.Generic;
using System.Linq;
class Demo {
static void Main() {
List<string> myList = new List<string>();
myList.Add("truck");
myList.Add("bus");
myList.Add("bicycle");
myList.Add("motorbike");
var myLen = from element in myList
orderby element.Length
select element;
Console.WriteLine("Ascending order...");
foreach (string str in myLen){
Console.WriteLine(str);
}
myLen = from element in myList
orderby element.Length descending
select element;
Console.WriteLine("Descending order...");
foreach (string str in myLen) {
Console.WriteLine(str);
}
}
} 출력
Ascending order... bus truck bicycle motorbike Descending order... motorbike bicycle truck bus