프로그래밍/(도서)C#코딩의기술-실전
[C#] 코딩의기술(실전편) - 3.3 OrderBy VS Sort
Victory_HA
2022. 3. 31. 22:31
Orderby 메서드
- 시퀀스의 요소를 오름차순으로 정렬합니다.
- 원래 데이터를 유지하면서 요소를 정렬합니다.
Sort 메서드
- 시퀀스의 요소를 오름차순으로 정렬합니다.
- 원래 데이터를 제거하고, 정렬 된 새로운 데이터를 넣습니다.
사용예제
class Program
{
static void Main(string[] args)
{
int[] sortArray = { 3, 1, 2 };
int[] orderByArray = { 3, 1, 2 };
SortAndOutput(sortArray);
PrintArray(sortArray);
OrderByAndOutput(orderByArray);
PrintArray(orderByArray);
Console.ReadKey();
}
private static void PrintArray(int[] arr)
{
Console.Write($"\n원본 데이터 : ");
foreach (var item in arr)
{
Console.Write($"{item} ");
}
}
private static void SortAndOutput(int[] arr)
{
Array.Sort(arr);
Console.Write($"Sort() Method Sorting Array : ");
foreach (var item in arr)
{
Console.Write($"{item} ");
}
}
private static void OrderByAndOutput(int[] arr)
{
Console.Write("\n\n");
Console.Write($"OrderBy() Method Sorting Array : ");
foreach (var item in arr.OrderBy(c=>c))
{
Console.Write($"{item} ");
}
}
}
결과