예제 #1
0
        /// <summary>
        /// This method is inherited from the IComparer interface.  It compares the two objects passed using a case insensitive comparison.
        /// </summary>
        /// <param name="x">First object to be compared</param>
        /// <param name="y">Second object to be compared</param>
        /// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
        public int Compare(object x, object y)
        {
            // Cast the objects to be compared to ListViewItem objects
            var itemX = x is ListViewItem ? ((ListViewItem)x).SubItems[SortColumn].Text : x.ToString();
            var itemY = y is ListViewItem ? ((ListViewItem)y).SubItems[SortColumn].Text : y.ToString();

            // Compare the two items
            int compareResult = comparer.Compare(itemX, itemY);

            // Calculate correct return value based on object comparison
            if (Order == SortOrder.Ascending)
            {
                // Ascending sort is selected, return normal result of compare operation
                return(compareResult);
            }
            else if (Order == SortOrder.Descending)
            {
                // Descending sort is selected, return negative result of compare operation
                return(-compareResult);
            }
            else
            {
                // Return '0' to indicate they are equal
                return(0);
            }
        }
예제 #2
0
        /// <summary>
        /// This method is inherited from the IComparer interface.  It compares the two objects passed using a case insensitive comparison.
        /// </summary>
        /// <param name="x">First object to be compared</param>
        /// <param name="y">Second object to be compared</param>
        /// <returns>The result of the comparison. "0" if equal, negative if 'x' is less than 'y' and positive if 'x' is greater than 'y'</returns>
        public int Compare(Chart x, Chart y)
        {
            // Cast the objects to be compared to ListViewItem objects
            string itemX = string.Empty, itemY = string.Empty;

            switch (SortType)
            {
            case ChartSortType.Title:
                itemX = x.Title;
                itemY = y.Title;
                break;

            case ChartSortType.Artist:
                itemX = x.Title;
                itemY = y.Title;
                break;

            case ChartSortType.Pattern:
                itemX = x.Pattern;
                itemY = y.Pattern;
                break;

            default: break;
            }

            // Compare the two items
            int compareResult = comparer.Compare(itemX, itemY);

            // Calculate correct return value based on object comparison
            if (Order == SortOrder.Ascending)
            {
                // Ascending sort is selected, return normal result of compare operation
                return(compareResult);
            }
            else if (Order == SortOrder.Descending)
            {
                // Descending sort is selected, return negative result of compare operation
                return(-compareResult);
            }
            else
            {
                // Return '0' to indicate they are equal
                return(0);
            }
        }