Skip to content

Commit

Permalink
Fix checkbox sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Feb 22, 2021
1 parent ae3dd44 commit 2ec5366
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions GUI/Controls/ManageMods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,6 +1246,15 @@ private int CompareRows(DataGridViewRow a, DataGridViewRow b)
return CompareColumn(a, b, ModName);
}

/// <summary>
/// Compare two rows based on one of their columns
/// </summary>
/// <param name="a">First row</param>
/// <param name="b">Second row</param>
/// <param name="col">The column to compare</param>
/// <returns>
/// -1 if a&lt;b, 1 if a&gt;b, 0 if a==b
/// </returns>
private int CompareColumn(DataGridViewRow a, DataGridViewRow b, DataGridViewColumn col)
{
GUIMod gmodA = a.Tag as GUIMod;
Expand All @@ -1256,15 +1265,21 @@ private int CompareColumn(DataGridViewRow a, DataGridViewRow b, DataGridViewColu
var cellB = b.Cells[col.Index];
if (col is DataGridViewCheckBoxColumn cbcol)
{
// Checked < non-"-" text < unchecked < "-" text
if (cellA is DataGridViewCheckBoxCell checkboxA)
{
return cellB is DataGridViewCheckBoxCell checkboxB
? -((bool)checkboxA.Value).CompareTo((bool)checkboxB.Value)
: -1;
? -((bool)checkboxA.Value).CompareTo((bool)checkboxB.Value)
: (bool)checkboxA.Value || ((string)cellB.Value == "-") ? -1
: 1;
}
else
{
return cellB is DataGridViewCheckBoxCell ? 1: 0;
return cellB is DataGridViewCheckBoxCell ? -CompareColumn(b, a, col)
: (string)cellA.Value == (string)cellB.Value ? 0
: (string)cellA.Value == "-" ? 1
: (string)cellB.Value == "-" ? -1
: ((string)cellA.Value).CompareTo((string)cellB.Value);
}
}
else
Expand Down

0 comments on commit 2ec5366

Please sign in to comment.