I'm fairly new in C# and just encountered a weird behavior.
This is a piece of script that should sort array of strings alphabetically (A-Z)
This is the array:
IFF-7/12
IFA-7/11
IFF-7/8
IFF-7/2
IFF-7/11
IFF-7/1
IF-7/1
IFF-7/6
IFF-7/9
IFF-7/13
IFF-7/14
IF-7/2
So I suppose the outcome should be:
IF-7/1
IF-7/2
IFA-7/11
IFF-7/1
IFF-7/2
IFF-7/6
IFF-7/8
IFF-7/9
IFF-7/11
IFF-7/12
IFF-7/13
IFF-7/14
Program script:
for (int i = 0; i < faculty.GroupCount; i++)
{
for (int j = i + 1; j < faculty.GroupCount; j++)
{
if (faculty.Groups[j].Name.CompareTo(faculty.Groups[i].Name) < 0)
{
temp = faculty.Groups[i];
faculty.Groups[i] = faculty.Groups[j];
faculty.Groups[j] = temp;
}
}
}
But the actual outcome is
IF-7/1
IF-7/2
IFA-7/11
IFF-7/1
IFF-7/11
IFF-7/12
IFF-7/13
IFF-7/14
IFF-7/2
IFF-7/6
IFF-7/8
IFF-7/9
I see what it's doing but I have no idea why. Also I can't use any of the libraries or methods like .sort .
Thanks for any help.
IComparer<T>instead : msdn.microsoft.com/en-us/library/8ehhxeaf(v=vs.110).aspxCompareTo). There are multiple older question on that natural sorting: 1, 2, 3. You might want to check them out and apply the code there to your particular problem.-sorts after letters, while in fact it sorts before letters, and even before digits (its hex code is 0x45, while the code of zero'0'is 0x48).