private IEnumerable<DisplayElement> Sort(IEnumerable<DisplayElement> tmp)
{
// if no columns to sort by, then just return the tmp variable
if (SortColumns == null || SortColumns.Count == 0)
{
return tmp;
}
IOrderedEnumerable<DisplayElement> sortedList = null;
bool isFirstTimeThrough = true;
foreach (string columnName in SortColumns.Keys)
{
Func<DisplayElement, object> columnValue = null;
string sortOptions = SortColumns[columnName];
// need to copy the column name, or it executes against the wrong column when we actually do the sort
string copyOfColumnName = columnName;
// sort datetime values separately
if (sortOptions.Contains("DateTime"))
{
columnValue = t => ParseDate(t.GetColumnValue(copyOfColumnName));
}
else
{
columnValue = t => t.GetColumnValue(copyOfColumnName);
}
// and sort based on order
if (sortOptions.Contains("ASC"))
{
sortedList = (isFirstTimeThrough) ? tmp.OrderBy(columnValue):sortedList.ThenBy(columnValue);
isFirstTimeThrough = false;
}
else if (sortOptions.Contains("DESC"))
{
sortedList = (isFirstTimeThrough) ? tmp.OrderByDescending(columnValue):sortedList.ThenByDescending(columnValue);
isFirstTimeThrough = false;
}
}
return sortedList;
}