C#/C#
C# : Sort (array 정렬하기)
CosmosProject
2022. 3. 23. 14:46
728x90
반응형
Sort method는 Array를 정렬해줍니다.
using System;
class MyProgram
{
static void Main()
{
string[] test_arr = { "Pineapple", "Banana", "Apple", "Grape" };
Array.Sort(test_arr);
foreach (string t in test_arr)
{
Console.WriteLine(t);
}
}
}
-- Result
Apple
Banana
Grape
Pineapple
test_arr은 무작위로 array 속 요소들이 존재합니다.
여기에 Array.Sort를 적용하면 test_arr 속 요소들이 알파벳 기준 오름차순으로 정렬됩니다.
using System;
class MyProgram
{
static void Main()
{
int[] test_arr = { 5, 2, 7, 8, 3 };
Array.Sort(test_arr);
foreach (int t in test_arr)
{
Console.WriteLine(t);
}
}
}
-- Result
2
3
5
7
8
숫자로 된 array에도 적용할 수 있습니다.
728x90
반응형