Here is one way to parse and sort a string with comma-delimited numbers:

string input = "1,9,3,4,3,9,1,5,6,78,";
string[] stringArray = input.Split( new char[] { ',' }, 
    StringSplitOptions.RemoveEmptyEntries );
int length = stringArray.Length;
int[] intArray = new int[length];
for (int i = 0; i < length; i++)
{
    try
    {
        intArray[i] = Convert.ToInt32( stringArray[i] );
    }
    catch (Exception)
    {
        // ignore
    }
}
Array.Sort( intArray );

Note how you must convert the numeric strings to integers BEFORE sorting.  Otherwise you will end up with the sort order 6, 78, 9, for example.  The Convert.ToInt32 method is enclosed in a try-catch block in case the numeric string does not contain a number, in which case it will default to 0.

Share and Enjoy:
  • Digg
  • Twitter
  • Facebook
  • Reddit
  • StumbleUpon
  • LinkedIn
  • Google Bookmarks
  • Slashdot