We all use <
, <=
, >
, and >=
with integers and floating point values all the time. It just works and it’s built into basically every programming language. These simple operators suddenly become quite a pain when you start wanting to compare other objects. IComparable
seems to make it easier, but there’s some trickiness when you start dealing with null
objects. Today’s article explores this and ends up with some handy utility functions to take some of the gotchas out of comparing.
Posts Tagged icomparable
List<T>
(and SafeList
) have a great feature for fast lookups: BinarySearch
. However, the list needs to be sorted in order to use it. You could call Sort()
first, but that would give back all the performance you got with BinarySearch
. It’s better to just keep the list sorted all the time. Unfortunately, there is no function on IList<T>
, List<T>
, or SafeList
to efficiently insert an item into a list that’s already sorted. Today’s article presents an extension function that adds this functionality on to IList<T>
and even the non-generic IList
so your list will always be sorted for quick lookups with BinarySearch
. Read on for the code, unit tests, and a performance test showing the advantages you stand to gain.