Posts Tagged icomparable

Comparing Null Objects

Tags: , , ,

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.

Read the rest of this article »

No Comments

Efficiently Keeping Lists Sorted

Tags: , , , , ,

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.

Read the rest of this article »

3 Comments