public static int[] SortArray(int[] characters, string Order)
{
if (Order == "Desc")
{
for (int i = 0; i < characters.Length; i++)
{
for (int j = i; j < characters.Length; j++)
{
if ((int)characters[j] > (int)characters[i])
{
int tempChar = characters[i];
characters[i] = characters[j];
characters[j] = tempChar;
}
}
}
}
else
{
for (int i = 0; i < characters.Length; i++)
{
for (int j = i; j < characters.Length; j++)
{
if ((int)characters[j] < (int)characters[i])
{
int tempChar = characters[i];
characters[i] = characters[j];
characters[j] = tempChar;
}
}
}
}
return characters;
}
///calling function
int[] characters = new int[] { 1,9,2,10,5 };
characters = Program.SortArray(characters, "Asc");
string str = "";
for (int i = 0; i < characters.Length; i++)
{
str += characters[i].ToString();
}
Console.WriteLine(str);
Console.ReadLine();
Thursday, January 27, 2011
Subscribe to:
Posts (Atom)