I would believe that they will not be evil, but they are not always good. (MSDN samples are sometimes quite terrible.)
For example, here is some code in the example you gave:
compareResult = ObjectCompare.Compare (listviewX.SubItems[ColumnToSort].Text, listviewY.SubItems[ColumnToSort].Text); // Calculate correct return value based on object comparison if (OrderOfSort == SortOrder.Ascending) { // Ascending sort is selected, return normal result of compare operation return compareResult; } else if (OrderOfSort == SortOrder.Descending) { // Descending sort is selected, return negative result of compare operation return (-compareResult); } else { // Return '0' to indicate they are equal return 0; }
Now there are two problems:
- Why is it considered that it has a comparator without sorting? This must be a constructor parameter, confirmed at the point of construction of the IMO.
- You should not simply deny the result of a single comparison in order to perform a “reverse comparison”. This breaks if the result of the first comparison is
int.MinValue - because -int.MinValue == int.MinValue . It is better to change the arguments used to perform the initial comparison.
There are other things that I would touch on in this code, but these two should be enough to express my point of view.
I sincerely agree with other answers, including: - Check copyright / license, etc. Any code you want to use. - Make sure you understand everything you want to use.
Jon skeet
source share