728x90
반응형
728x170
▶ ArrayExtension.cs
namespace TestProject;
/// <summary>
/// 배열 확장
/// </summary>
public static class ArrayExtension
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 정렬하기 - OrderBy<TElement>(sourceArray, keySelectorFunction)
/// <summary>
/// 정렬하기
/// </summary>
/// <typeparam name="TElement">배열 요소 타입</typeparam>
/// <param name="sourceArray">2차원 소스 배열</param>
/// <param name="keySelectorFunction">키 선택자 함수</param>
/// <returns>정렬된 2차원 배열</returns>
public static TElement[,] OrderBy<TElement>(this TElement[,] sourceArray, Func<TElement[], TElement> keySelectorFunction)
{
return sourceArray.ConvertToSingleDimension().OrderBy(keySelectorFunction).ConvertToMultiDimensional();
}
#endregion
#region 역순으로 정렬하기 - OrderByDescending<T>(sourceArray, keySelectorFunction)
/// <summary>
/// 역순으로 정렬하기
/// </summary>
/// <typeparam name="TElement">배열 요소 타입</typeparam>
/// <param name="sourceArray">2차원 소스 배열</param>
/// <param name="keySelectorFunction"></param>
/// <returns>정렬된 2차원 배열</returns>
public static TElement[,] OrderByDescending<TElement>(this TElement[,] sourceArray, Func<TElement[], TElement> keySelectorFunction)
{
return sourceArray.ConvertToSingleDimension().OrderByDescending(keySelectorFunction).ConvertToMultiDimensional();
}
#endregion
#region 출력하기 - Print<T>(sourceArray)
/// <summary>
/// 출력하기
/// </summary>
/// <param name="sourceArray">2차원 소스 배열</param>
public static void Print<T>(this T[,] sourceArray)
{
int rowCount = sourceArray.GetLength(0);
int columnCount = sourceArray.GetLength(1);
for(int row = 0; row < rowCount; row++)
{
for(int column = 0; column < columnCount; column++)
{
Console.Write(sourceArray[row, column]);
}
Console.WriteLine();
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Private
#region 1차원으로 변환하기 - ConvertToSingleDimension<TElement>(sourceArray)
/// <summary>
/// 1차원으로 변환하기
/// </summary>
/// <typeparam name="TElement">배열 요소 타입</typeparam>
/// <param name="sourceArray">2차원 소스 배열</param>
/// <returns>1차원 배열 열거 가능형</returns>
private static IEnumerable<TElement[]> ConvertToSingleDimension<TElement>(this TElement[,] sourceArray)
{
TElement[] rowArray;
for(int row = 0; row < sourceArray.GetLength(0); row++)
{
rowArray = new TElement[sourceArray.GetLength(1)];
for(int column = 0; column < sourceArray.GetLength(1); ++column)
{
rowArray[column] = sourceArray[row, column];
}
yield return rowArray;
}
}
#endregion
#region 다차원으로 변환하기 - ConvertToMultiDimensional<TElement>(sourceEnumerable)
/// <summary>
/// 다차원으로 변환하기
/// </summary>
/// <typeparam name="TElement">배열 요소 타입</typeparam>
/// <param name="sourceEnumerable">1차원 소스 배열 가능형</param>
/// <returns>2차원 배열</returns>
private static TElement[,] ConvertToMultiDimensional<TElement>(this IEnumerable<TElement[]> sourceEnumerable)
{
TElement[,] targetArray;
TElement[][] temporaryArray;
int columnCount;
temporaryArray = sourceEnumerable.ToArray();
columnCount = (temporaryArray.Length > 0) ? temporaryArray[0].Length : 0;
targetArray = new TElement[temporaryArray.Length, columnCount];
for(int row = 0; row < temporaryArray.GetLength(0); row++)
{
for(int column = 0; column < columnCount; column++)
{
targetArray[row, column] = temporaryArray[row][column];
}
}
return targetArray;
}
#endregion
}
300x250
▶ Program.cs
namespace TestProject;
/// <summary>
/// 프로그램
/// </summary>
class Program
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Private
#region 프로그램 시작하기 - Main()
/// <summary>
/// 프로그램 시작하기
/// </summary>
private static void Main()
{
Console.WriteLine("소스 배열");
Console.WriteLine();
int[,] sourceArray = new int[3, 3]
{
{ 1, 4, 2 },
{ 4, 5, 1 },
{ 7, 3, 8 }
};
sourceArray.Print();
Console.WriteLine();
Console.WriteLine("2차원 배열에서 첫번째 컬럼 값을 기준으로 정렬한다 :");
Console.WriteLine();
int[,] targetArray1 = sourceArray.OrderBy(x => x[0]);
targetArray1.Print();
Console.WriteLine();
Console.WriteLine("2차원 배열에서 두번째 컬럼 값을 기준으로 정렬한다 :");
Console.WriteLine();
int[,] targetArray2 = sourceArray.OrderBy(x => x[1]);
targetArray2.Print();
Console.WriteLine();
Console.WriteLine("2차원 배열에서 세번째 컬럼 값을 기준으로 정렬한다 :");
Console.WriteLine();
int[,] targetArray3 = sourceArray.OrderBy(x => x[2]);
targetArray3.Print();
}
#endregion
}
728x90
반응형
그리드형(광고전용)
'C# > Common' 카테고리의 다른 글
[C#/COMMON/.NET6] TimeSpan 구조체 : 디폴트 함수 인자 정의하기 (0) | 2022.09.01 |
---|---|
[C#/COMMON/.NET6] String 클래스 : Join 정적 메소드를 사용해 문자열 결합하기 (0) | 2022.09.01 |
[C#/COMMON/.NET6] 이진 검색 트리(Binary Search Tree) 사용하기 (0) | 2022.08.26 |
[C#/COMMON/.NET6] 제곱근 계산하기 (Math.Sqrt 정적 메소드 미사용) (0) | 2022.08.25 |
[C#/COMMON/.NET6] implicit operator 키워드 : 암시적 변환 사용하기 (0) | 2022.08.19 |
[C#/COMMON/.NET6] 가변 배열 사용하기 (0) | 2022.08.19 |
[C#/COMMON/.NET6] Array 클래스 : Sort 정적 메소드를 사용해 자연 정렬하기 (0) | 2022.08.15 |
[C#/COMMON] Regex 클래스 : Replace 정적 메소드를 사용해 다중 공백 문자열을 단일 공백 문자열로 대체하기 (0) | 2022.07.28 |
[C#/COMMON/.NET6] String 클래스 : 다중 공백 문자열을 단일 공백 문자열로 대체하기 (0) | 2022.07.28 |
[C#/COMMON] Regex 클래스 : Replace 메소드를 사용해 다중 공백 문자열을 단일 공백 문자열로 대체하기 (0) | 2022.07.28 |
댓글을 달아 주세요