how to use array.sort in a for loop c# code example
Example: c# sort for loop
-----------------------------------------------option 1 (int Desc)
public static void Main(string[] args)
{
int[] arr = { 5, 4, 8, 11, 1 };
Sorting(arr);
}
public static void Sorting(int[] arr)
{
int length = arr.Length;
for (int i = 0; i < length - 1; i++)
{
if (arr[i] < arr[i + 1])
{
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
i = -1;
}
}
foreach (int item in arr)
{
Console.WriteLine(item);
}
}
--------------------------------------------------Option 2 (string Asc)
static void Main(string[] args)
{
Sorting("zxeba");
}
public static void Sorting(string s)
{
var arr = s.ToArray();
for (int i = 0; i < arr.Length; i++)
{
for (int j = 0; j < arr.Length - 1; j++)
{
if (arr[j+1] < arr[j])
{
char temp = arr[j+1];
arr[j+1] = arr[j];
arr[j] = temp;
}
}
}
string result = String.Join("", arr);
Console.WriteLine(result);
}