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