internal IndexField[] ParseSortString(string sortString)
{
IndexField[] indexDesc = Array.Empty<IndexField>();
if ((null != sortString) && (0 < sortString.Length))
{
string[] split = sortString.Split(new char[] { ',' });
indexDesc = new IndexField[split.Length];
for (int i = 0; i < split.Length; i++)
{
string current = split[i].Trim();
// handle ASC and DESC.
int length = current.Length;
bool descending = false;
if (length >= 5 && string.Compare(current, length - 4, " ASC", 0, 4, StringComparison.OrdinalIgnoreCase) == 0)
{
current = current.Substring(0, length - 4).Trim();
}
else if (length >= 6 && string.Compare(current, length - 5, " DESC", 0, 5, StringComparison.OrdinalIgnoreCase) == 0)
{
descending = true;
current = current.Substring(0, length - 5).Trim();
}
// handle brackets.
if (current.StartsWith("[", StringComparison.Ordinal))
{
if (current.EndsWith("]", StringComparison.Ordinal))
{
current = current.Substring(1, current.Length - 2);
}
else
{
throw ExceptionBuilder.InvalidSortString(split[i]);
}
}
// find the column.
DataColumn column = Columns[current];
if (column == null)
{
throw ExceptionBuilder.ColumnOutOfRange(current);
}
indexDesc[i] = new IndexField(column, descending);
}
}
return indexDesc;
}