Numeric String Sort in C#

Scenario: you have a List<string> collection where the contents could be alpha, numeric, or alphanumeric. If you just sort the collection using the built-in Sort() method, it will use string.CompareTo(), treating each item in the collection as a string. Thus a collection with the following values ["1", "44", "22", 4", "5"] will end up being sorted as ["1", "22", "4", "44", "5"] – even though you might want the items to be treated as numbers (in which case they would be sorted as ["1", "4", "5", "22", "44"].

The solution to this is to use a custom comparer class. I put the following together:

public class NumericStringSort : IComparer {

    int IComparer.Compare(string a, string b) {
        decimal aDec;
        decimal bDec;
        if (decimal.TryParse(a, out aDec) && decimal.TryParse(b, out bDec)) {
            return aDec.CompareTo(bDec);
        } else {
            return a.CompareTo(b);

    public static IComparer NumericStringSorter() {
        return (IComparer) new NumericStringSort();

As you can see, the class implements the IComparer interface. If both of the strings can be converted into decimals, then the decimal comparison is used. Otherwise the string comparison is used.

You can see a usage example through the following set of unit tests (as you can see, it also handles mixed alphanumeric sets gracefully, sorting all numeric items to the front of the list in numeric order, followed by all alpha entries in alphabetical order):

public class NumericStringSortTests {

    public void TestNumericSort_AllNumeric() {
        List items = new List { "1", "44", "22", "4", "5" };
        List expectedItems = new List { "1", "4", "5", "22", "44" };

    public void TestNumericSort_MixesAlphaNumeric() {
        List items = new List { "a", "c", "d", "b", "1", "2", "22", "3" };
        List expectedItems = new List { "1", "2", "3", "22", "a", "b", "c", "d" };

    public void TestNumericSort_AllAlpha() {
        List items = new List { "a", "c", "d", "b" };
        List expectedItems = new List { "a", "b", "c", "d" };

This is something that I have needed on previous occasions – perhaps it may be of use to someone else.

Protect Yourself from Copyright Trolls

For blogs and online forums that excerpt content from other sites and post third-party content (like content), in order to enjoy “safe harbor” status under the DMCA, the site must register a contact for takedown notices. Costs $105 (plus postage) but can end up saving a bundle down the line. See this Wired article, which talks about a company called Righthaven which is buying up the rights to old newspaper material with the sole purpose of suing those who posted quotes from it online, and is targeting sites that do not have a registered DMCA Takedown contact:

Under the Digital Millennium Copyright Act, a website enjoys effective immunity from civil copyright liability for user content, provided they, promptly remove infringing material at the request of a rightsholder. That’s how sites like YouTube are able to exist, and why allows users to post comments to our stories without fear that a single user’s cut-and-paste will cost us $150,000 in court.

But to dock in that legal safe harbor, a site has to, among other things, register an official contact point for DMCA takedown notices, a process that involves filling out a form and mailing a check to the government. An examination of Righthaven’s lawsuits targeting user content suggests it’s specifically going after sites that failed to fill out that paperwork.

“The DMCA is a good deterrent from being sued,” says Kurt Opsahl, a staff attorney with the Electronic Frontier Foundation, “Complying with conditions of eligibility for the safe harbor is a good thing to do. It probably will prevent somebody from suing you in the first place.”

(Hopefully that excerpt is short enough to be considered fair use, combined with the citation and link. Would be kind of ironic if I exposed myself to a legal troubles by quoting from an article that talks about how you can avoid such things). An alternative is to use a hosted service for your blogging, who will have already registered a takedown contact and will handle all DMCA notices.

Handling SSN Input Properly

I just logged into my Google Adsense account. After 5+ years, I finally earned enough to get a payment. Only to discover that I had forgotten to update my mailing address, and that the check was sent a month ago to an address to which I no longer have access (if you find yourself in this situation, just cancel the check, and set up electronic funds transfer – much easier). In the process of updating my Adsense account, I put in my social security number so that the proper tax forms can be filed. I just entered it in the format of 123456789, since after all, a social security number is nine digits long. When I submitted the form, I got the following validation error:

So they want me to insert dashes into the SSN so that it is in the format of XXX-XX-XXXX (the format in which a social security number is normally written). That is not so hard for me to do. But why should I have to do that? They are already validating that I have the proper number of digits. So once they know that I entered nine digits, why can’t they just enter the dashes for me? No reason to bother the user with inanities like this. (One could also ask why they need to store the SSN in this format – storing them as nine digits in an int field is probably more efficient than storing them in a text field.)

Hot Computer = Slow Computer

I have had a Dell Latitude D830 laptop for the past couple of years. Core2 Duo, 2.5 Ghz, 4GB RAM, Vista64. In the beginning it was flying. Fastest computer I have ever had. But then as the months went by, it started to slow. Recently, performance has been horrible. Looking at the system resource, memory was OK. But sometimes in Visual Studio I had to wait for the IDE to catch up with what I had typed (running Firefox with 20 tabs, Visual Studio 2008, Outlook, Excel and Sql Management Studio, but I used to have no problem with all of them open at the same time).

After some Googling, I saw that the first thing to check in my case would be the system temperature. If the system is overheating then the processors cant run to their full potential. And lo and behold, when I lifted up the laptop, there was dust all over the Fan intake (and the laptop was so close to the desk that even with it clean hardly any air would get in). So I cleaned off the desk and underside of the laptop, and propped up the back of the computer (I knew that I would get some good use out of this). Downloaded and installed Speed Fan and watched all of the core temperature numbers drop. Computer performance immediately went up. (I also took off the keyboard and checked to see if there was a buildup of dust inside the fan, but it was clean. Ran the fan diagnostic and it was ok as well). The computer is still running hot (still something to work on), but I have definitely learned my lesson – high performance laptops really suffer in the heat. Keep them cool!

Vaccines, MMR and the Withdrawn Lancet Study

A single study published in 1998 in the prestigious medical journal Lancet led to a large reduction in the rate of vaccinations in UK with MMR (measles-mumps-rubella) vaccine and consequent increases in diseases and number of deaths preventable by this outstanding vaccine. The reduced rate of vaccination stimulated by this publication spilled over to many other developed countries and to vaccines besides MMR.

In the years following publication, this work could not be duplicated by any other research group. It also became apparent that there was a conflict of interest in that the senior author of the paper had accepted funding from a lawyer who files lawsuits for damages on behalf of families of vaccinated children, including the published study.
As a result, most of the co-authors of that paper withdrew their designation as co-authors.

Now, Lancet has finally withdrawn the paper (with a short and hard-to-understand statement that seems to avoid mentioning the real controversy). Better late than never. However, it might be that no other publication has been as damaging to the field of vaccines and to public health as this one – now officially discredited.

Please pass this note along to anyone you know who is afraid of vaccinating themselves or their children, with any vaccine and especially with MMR vaccine. As quoted in this article “”It’s very easy to scare people; it’s very hard to unscare them.”

(The above was sent to me by my father, who has worked in the Vaccine industry for the last thirty years. Not the normal fare for this blog, but I thought that it is worth sharing, especially considering how Mumps outbreaks are back in the news. And since billg did just pledge $10 billion to vaccines through his foundation, the topic is almost tech-related).