/// <summary>
/// Sort Int Array
/// </summary>
/// <param name="sortBy">A- Ascendig, D- Descending order</param>
/// <param name="arr">int array to sort</param>
/// <returns>Sorted Array </returns>
/// Sorting without using the build sort in C# and using unlimited int array parameters using the params keyword
public static int [] SortArray(char sortBy, params int [] arr)
{
int tmp;
for(int x =0; x< arr.Length - 1; x++)
{
// traverse i + 1
for(int j =x + 1; j < arr.Length; j++)
{
//compare and swap
if (sortBy.Equals('A'))
{
if (arr[x] > arr[j])
{
tmp = arr[x];
arr[x] = arr[j];
arr[j] = tmp;
}
}else if(sortBy.Equals('D'))
{
if (arr[x] < arr[j])
{
tmp = arr[x];
arr[x] = arr[j];
arr[j] = tmp;
}
}else
{
if (arr[x] > arr[j])
{
tmp = arr[x];
arr[x] = arr[j];
arr[j] = tmp;
}
}
}
}
return arr;
}
// See https://aka.ms/new-console-template for more information
string[] myStrArr = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
int[] myIntArr = {5, 6, 4, 7, 85, 9, 2, 12, 3, 1};
// or use Array.Reverse() for DESC
Array.Sort(myStrArr);
Array.Sort(myIntArr);
// Array.Sort() for ASC
myStrArr.ToList().ForEach(i => Console.WriteLine(i.ToString()));
myIntArr.ToList().ForEach(i => Console.WriteLine(i));