1
Reply

Sorting an Array of strings - minor issue.

Arfan Dhillon

Arfan Dhillon

Oct 8 2009 11:13 AM
2.9k

Hi guys.
The code below reads in a string, splits the string into an array with every comma. Then alphabetically sorts the array and re-joins the string with a comma in between (and also removes that last comma afterwards).
 
 

static string alphabeticallyOrderString(string lineholderX)
{
string tmpLength = "";

string[] stringSplit = Regex.Split(lineholderX, ",");
Array.Sort(stringSplit);

lineholderX =
"";
for (int i = 0; i < stringSplit.Length; i++)
{
if (stringSplit[i] != "")
{
tmpLength =
"";
lineholderX += tmpLength + stringSplit[i] +
",";
}
}
char lastComma = ',';
lineholderX = lineholderX.TrimEnd(lastComma);
return lineholderX;
}

If my input string is " UE-CA , UF-BA , UF-AB , OF-AB ," , I want my output string to be " UE-CA , UF-AB , OF-AB , UF-BA". However my code below will output it as " OF-AB , UE-CA , UF-AB , UF-BA". The problem occurs with OF-AB f.
I would like to sort the array by ignoring the first letter of the each string, but still output the same result above.
I appreciate any help and thank you in advance. Also, I know my coding method might be questionable, but it sort of works.

Answers (1)