I am trying to sort an ArrayList
filled with strings alphabetically. When I call the Sort method the alphabet seems to sort fine, however when there are numbers involved the Sort method seems to sort incorrectly.
Take this code for example:
ArrayList list = new ArrayList ();
list.Add ("img149");
list.Add ("img15");
list.Add ("a");
list.Sort ();
for (int i = 0; i < list.Count (); i++) {
Console.WriteLine (list [i]);
}
This seems to print:
a
img_149
img_15
the string "a" is sorted fine, but the two other strings are incorrectly sorted. I think I understand why this is as "4" comes before "5" however, 149 is really higher than 15 in which case the string with the 15 should print first.
For my situation, I never know exactly what the strings will be (the strings represent file names in my program), and it is crucial the names are sorted alphabetically with the numbers in order (1, 2, 150, 300, etc.). Does anyone have any ideas on how to rectify this?