Skip to content

Commit

Permalink
Merge #1420: Numerical columns can now be sorted as numbers.
Browse files Browse the repository at this point in the history
* 1420_adjust:
  Simply colum sorting to use methods rather than closures
  Implemented a int sorter Moved the creation of the 3 different sorter kinds into different methods for: string based, integer based and checkbox based fixes #1419
  • Loading branch information
pjf committed Sep 10, 2015
2 parents 5864cec + cd47a9b commit 07157d6
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 22 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ All notable changes to this project will be documented in this file.

### Bugfixes

- [GUI] Numerical columns can now be sorted numerically. (Postremus, #1420)

### Features

- [GUI] Updating the list of available mods will no longer clear user selections. (Postremus, #1402)
Expand Down
85 changes: 63 additions & 22 deletions GUI/MainModList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,79 @@ private void UpdateFilters(Main control)

private IEnumerable<DataGridViewRow> _SortRowsByColumn(IEnumerable<DataGridViewRow> rows)
{
var get_row_mod_name = new Func<DataGridViewRow, string>(row => ((GUIMod) row.Tag).Name);
Func<DataGridViewRow, string> sort_fn;

// XXX: There should be a better way to identify checkbox columns than hardcoding their indices here
if (this.m_Configuration.SortByColumnIndex < 2)
{
sort_fn = new Func<DataGridViewRow, string>(row =>
{
var cell = row.Cells[this.m_Configuration.SortByColumnIndex];
if (cell.ValueType == typeof (bool))
{
return (bool) cell.Value ? "a" : "b";
}
// It's a "-" cell so let it be ordered last
return "c";
});
return Sort(rows, CheckboxSorter);
}
else
// XXX: Same for Integer columns
else if (this.m_Configuration.SortByColumnIndex == 7)
{
sort_fn =
new Func<DataGridViewRow, string>(
row => row.Cells[this.m_Configuration.SortByColumnIndex].Value.ToString());
return Sort(rows, IntegerSorter);
}
// Update the column sort glyph
this.ModList.Columns[this.m_Configuration.SortByColumnIndex].HeaderCell.SortGlyphDirection =
this.m_Configuration.SortDescending ? SortOrder.Descending : SortOrder.Ascending;
return Sort(rows, DefaultSorter);
}

private IEnumerable<DataGridViewRow> Sort<T>(IEnumerable<DataGridViewRow> rows, Func<DataGridViewRow, T> sortFunction)
{
var get_row_mod_name = new Func<DataGridViewRow, string>(row => ((GUIMod)row.Tag).Name);
DataGridViewColumnHeaderCell header =
this.ModList.Columns[this.m_Configuration.SortByColumnIndex].HeaderCell;

// The columns will be sorted by mod name in addition to whatever the current sorting column is
if (this.m_Configuration.SortDescending)
{
return rows.OrderByDescending(sort_fn).ThenBy(get_row_mod_name);
header.SortGlyphDirection = SortOrder.Descending;
return rows.OrderByDescending(sortFunction).ThenBy(get_row_mod_name);
}
return rows.OrderBy(sort_fn).ThenBy(get_row_mod_name);

header.SortGlyphDirection = SortOrder.Ascending;
return rows.OrderBy(sortFunction).ThenBy(get_row_mod_name);
}

/// <summary>
/// Transforms a DataGridViewRow's into a generic value suitable for sorting.
/// Uses this.m_Configuration.SortByColumnIndex to determine which
/// field to sort on.
/// </summary>
private string DefaultSorter(DataGridViewRow row)
{
return row.Cells[this.m_Configuration.SortByColumnIndex].Value.ToString();
}

/// <summary>
/// Transforms a DataGridViewRow's checkbox status into a value suitable for sorting.
/// Uses this.m_Configuration.SortByColumnIndex to determine which
/// field to sort on.
/// </summary>
private string CheckboxSorter(DataGridViewRow row)
{
var cell = row.Cells[this.m_Configuration.SortByColumnIndex];
if (cell.ValueType == typeof(bool))
{
return (bool)cell.Value ? "a" : "b";
}
// It's a "-" cell so let it be ordered last
return "c";
}

/// <summary>
/// Transforms a DataGridViewRow into an integer suitable for sorting.
/// Uses this.m_Configuration.SortByColumnIndex to determine which
/// field to sort on.
/// </summary>
private int IntegerSorter(DataGridViewRow row)
{
var cell = row.Cells[this.m_Configuration.SortByColumnIndex];

if (cell.Value.ToString() == "N/A")
return -1;
else if (cell.Value.ToString() == "1<KB")
return 0;

int result = -2;
int.TryParse(cell.Value as string, out result);
return result;
}

private void _UpdateFilters()
Expand Down

0 comments on commit 07157d6

Please sign in to comment.