diff --git a/GUI/MainModList.cs b/GUI/MainModList.cs index 878e865f98..7a0b550146 100644 --- a/GUI/MainModList.cs +++ b/GUI/MainModList.cs @@ -18,38 +18,71 @@ private void UpdateFilters(Main control) private IEnumerable _SortRowsByColumn(IEnumerable rows) { - var get_row_mod_name = new Func(row => ((GUIMod) row.Tag).Name); - Func 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(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( - 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 Sort(IEnumerable rows, Func sortFunction) + { + var get_row_mod_name = new Func(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 CreateDefaultSorter() + { + return new Func( + row => row.Cells[this.m_Configuration.SortByColumnIndex].Value.ToString()); + } + + private Func CreateCheckboxSorter() + { + return new Func(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 CreateIntegerSorter() + { + return new Func(row => + { + var cell = row.Cells[this.m_Configuration.SortByColumnIndex]; + + if (cell.Value.ToString() == "N/A") + return -1; + else if (cell.Value.ToString() == "1