CompareStr, CompareText, CompareDate, etc.greenspun.com : LUSENET : OCI Best Practices : One Thread |
When implementing an TListView.OnCompare routine, or coding a TList or QuickSortStringsObject compare routine, one typically codes a compound if statement to compare strings or dates. However, for string, date or integer comparisons you don't need a compound if statement.
- If you are comparing strings use CompareStr or CompareText. You will usually use CompareText because it is not case sensitive.
- If you are comparing dates use CompareDate, which is a TOCIForm class method. It works just like the other compare routines: returning -1, 0 or 1 depending on whether the first date is less than, equal to, or greater than the second date.
- If you are comparing integers do the compare by subtracting the second value from the first. The result is a positive value is the first is greater than the second, zero if they are equal, and a negative number is it's smaller. The sort compare routines are only looking for negative, zero, or positive values, so this technique works fine.
-- Anonymous, August 27, 1998