Sort characters descending
Given a string, write a method that returns array of chars (ASCII characters) sorted in descending order.
Expected input and output
SortCharactersDescending("onomatopoeia") → tpoooonmieaa
SortCharactersDescending("fohjwf42os") → wsoojhff42
using System;
namespace CSharpExercises.Exercises.Strings
{
class SortCharactersDescendingTask
{
static char[] SortCharactersDescending(string str)
{
char[] charArray = str.ToCharArray();
char ch;
for (int i = 1; i < str.Length; i++)
{
for (int j = 0; j < str.Length - 1; j++)
{
if (charArray[j] < charArray[j + 1])
{
ch = charArray[j];
charArray[j] = charArray[j + 1];
charArray[j + 1] = ch;
}
}
}
return charArray;
}
static void Main(string[] args)
{
Console.WriteLine(SortCharactersDescending("Aliquam pulvinar aliquam libero, in fringilla erat.")); // vuuutrrrrqqponnnmmlllllliiiiiiigfeebaaaaaaA.,
Console.WriteLine(SortCharactersDescending("Thi2 12 5@mpl3 Str!nG~")); // ~trpnmlihTSG@53221!
}
}
}