public int Compare(object x, object y) { if (_objOrderOfSort == SortOrder.None) { return(0); } int intCompareResult; // Cast the objects to be compared to ListViewItem objects ListViewItem objListViewX = (ListViewItem)x; ListViewItem objListViewY = (ListViewItem)y; // Compare the two items if (_intColumnToSort == 0) { if (objListViewX is ListViewItemWithValue objListViewItemWithValueX && objListViewY is ListViewItemWithValue objListViewItemWithValueY && (objListViewItemWithValueX.Value is IComparable || objListViewItemWithValueY.Value is IComparable)) { if (objListViewItemWithValueX.Value is IComparable objXValue) { intCompareResult = objXValue.CompareTo(objListViewItemWithValueY.Value); } else { intCompareResult = -(objListViewItemWithValueY.Value as IComparable)?.CompareTo(objListViewItemWithValueX.Value) ?? 0; } } else { intCompareResult = CompareListViewItems.CompareTextAsDates(objListViewX, objListViewY); } }
public int Compare(object x, object y) { if (_objOrderOfSort == SortOrder.None) { return(0); } int intCompareResult; // Cast the objects to be compared to ListViewItem objects ListViewItem listviewX = (ListViewItem)x; ListViewItem listviewY = (ListViewItem)y; // Compare the two items if (_intColumnToSort == 0) { intCompareResult = CompareListViewItems.CompareTextAsDates(listviewX, listviewY); } else { string strX = listviewX?.SubItems[_intColumnToSort].Text.FastEscape('¥'); string strY = listviewY?.SubItems[_intColumnToSort].Text.FastEscape('¥'); if (decimal.TryParse(strX, System.Globalization.NumberStyles.Any, GlobalOptions.CultureInfo, out decimal decX) && decimal.TryParse(strY, System.Globalization.NumberStyles.Any, GlobalOptions.CultureInfo, out decimal decY)) { intCompareResult = decimal.Compare(decX, decY); } else { intCompareResult = string.Compare(strX, strY, true, GlobalOptions.CultureInfo); } } // Calculate correct return value based on object comparison if (_objOrderOfSort == SortOrder.Ascending) { return(intCompareResult); } return(-intCompareResult); }