Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GUI: Numerical columns can be sorted numerically #1420

Merged
merged 1 commit into from
Sep 10, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 55 additions & 22 deletions GUI/MainModList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,71 @@ 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, CreateCheckboxSorter());
}
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, CreateIntegerSorter());
}
// 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, CreateDefaultSorter());
}

private IEnumerable<DataGridViewRow> Sort<T>(IEnumerable<DataGridViewRow> rows, Func<DataGridViewRow, T> sortFunction)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet! Today I learnt the Func<> keyword in C#. :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@plague006 : I have no idea what I just watched, but it was amazing.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You learned about the func. 😄

{
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);
}

private Func<DataGridViewRow, string> CreateDefaultSorter()
{
return new Func<DataGridViewRow, string>(
row => row.Cells[this.m_Configuration.SortByColumnIndex].Value.ToString());
}

private Func<DataGridViewRow, string> CreateCheckboxSorter()
{
return 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";
});
}

private Func<DataGridViewRow, int> CreateIntegerSorter()
{
return new Func<DataGridViewRow, int>(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