private void HandleOrderByMatches(object sender, EventArgs e) { // Reverses meaning of int.CompareTo // depending on the current ordering int direction = _matchNumberOrdering ? 1 : -1; int CompareItems(ITreeGridItem item, ITreeGridItem otherItem) { int a = (item as SearchResultEntry)?.Matches ?? 0; int b = (otherItem as SearchResultEntry)?.Matches ?? 0; return(a.CompareTo(b) * direction); } _itemCollection.Sort(CompareItems); _matchNumberOrdering = !_matchNumberOrdering; if (_itemCollection.Any()) { tvwResultExplorer.ScrollToRow(0); } }