CÁC GIẢI PHÁP LẬP TRÌNH C# - Trang 212

212
Chương 6: Windows Form

public ListViewItemComparer(int columnIndex) {

Column = columnIndex;

}

public int Compare(object x, object y) {

ListViewItem listX = (ListViewItem)x;

ListViewItem listY = (ListViewItem)y;

if (Numeric) {

// Chuyển text thành số trước khi so sánh.

// Nếu chuyển đổi thất bại, sử dụng giá trị 0.

decimal listXVal, listYVal;

try {

listXVal = Decimal.Parse(listX.SubItems[Column].Text);

}

catch {

listXVal = 0;

}

try {

listYVal = Decimal.Parse(listY.SubItems[Column].Text);

}

catch {

listYVal = 0;

}

return Decimal.Compare(listXVal, listYVal);

}

else {

// Giữ nguyên text ở định dạng chuỗi

// và thực hiện so sánh theo thứ tự alphabetic.