CustomListView: Corregir comparador de columnas para que no falle ante items de lista sin las mismas columnas

This commit is contained in:
2014-09-09 13:15:58 +02:00
parent 9d77bb552b
commit ae8ce4a213

View File

@@ -57,41 +57,31 @@ namespace ServerExplorer.Controls
#region ListViewItemComparer #region ListViewItemComparer
class ListViewItemComparer : IComparer internal class ListViewItemComparer : IComparer
{ {
private int col; private int _col;
private bool ascending; private bool _ascending;
public void SetColumn(int col) public void SetColumn(int col)
{ {
if (this.col == col) _col = col;
{ _ascending = _col != col || !_ascending;
ascending = ascending ? false : true;
}
else
{
this.col = col;
ascending = true;
}
} }
public int Compare(object x, object y) public int Compare(object x, object y)
{ {
int returnVal = -1; var itemA = (ListViewItem) x;
if (ascending) var itemB = (ListViewItem) y;
if (itemA.SubItems.Count <= _col || itemB.SubItems.Count <= _col)
{ {
returnVal = String.Compare( return -1;
((ListViewItem)x).SubItems[col].Text,
((ListViewItem)y).SubItems[col].Text);
} }
else return _ascending
{ ? String.CompareOrdinal(itemA.SubItems[_col].Text, itemB.SubItems[_col].Text)
returnVal = String.Compare( : String.CompareOrdinal(itemB.SubItems[_col].Text, itemA.SubItems[_col].Text);
((ListViewItem)y).SubItems[col].Text,
((ListViewItem)x).SubItems[col].Text);
}
return returnVal;
} }
} }
#endregion #endregion
} }