Hi, i've got a array of many strings. How can I sort the strings by alphabet?
+6
A:
Sounds like you just want to use the Array.Sort
method.
Array.Sort(myArray)
There are many overloads, some which take custom comparers (classes or delegates), but the default one should do the sorting alphabetically (ascending) as you seem to want.
Noldorin
2010-06-30 11:30:43
+1 for mentioning its only the default behaviour. It won't sort reverse alphabetically, you need to trick it or implement your own sort.
Adam
2010-06-30 11:32:37
Or just .Sort() if they are in a List
simendsjo
2010-06-30 11:32:48
+1 Excellent answer, for more complex sorting I would take a look at the IComparable interface or Linq's Sort expression
armannvg
2010-06-30 12:45:41
+1
A:
class Program
{
static void Main()
{
string[] a = new string[]
{
"Egyptian",
"Indian",
"American",
"Chinese",
"Filipino",
};
Array.Sort(a);
foreach (string s in a)
{
Console.WriteLine(s);
}
}
}
Pranay Rana
2010-06-30 11:32:08