String.Compare Function (inbuilt) Logic in C#

Below is the algorithm will execute internally in .Net framework.

String.Compare() Function

[SecuritySafeCritical]

public virtual int Compare(string string1, string string2, CompareOptions options)

{

if (options == CompareOptions.OrdinalIgnoreCase)

{

return string.Compare(string1, string2, StringComparison.OrdinalIgnoreCase);

}

if ((options & CompareOptions.Ordinal) != CompareOptions.None)

{

if (options != CompareOptions.Ordinal)

{

throw new ArgumentException(Environment.GetResourceString("Argument_CompareOptionOrdinal"), "options");

}

return string.CompareOrdinal(string1, string2);

}

if ((options & ~(CompareOptions.StringSort | CompareOptions.IgnoreWidth | CompareOptions.IgnoreKanaType | CompareOptions.IgnoreSymbols | CompareOptions.IgnoreNonSpace | CompareOptions.IgnoreCase)) != CompareOptions.None)

{

throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"), "options");

}

if (string1 == null)

{

if (string2 == null)

{

return 0;

}

return -1;

}

if (string2 == null)

{

return 1;

}

return InternalCompareString(this.m_dataHandle, this.m_sortName, string1, 0, string1.Length, string2, 0, string2.Length, GetNativeCompareFlags(options));

}