jump to navigation

Case Insensitive or Not Case Sensitive? November 10, 2009

Posted by asalga in open source.
trackback

I’m very confused right now as to what exactly sort from the Processing library should be returning. The manual page says the the sort is not case sensitive, yet when I sort this array:

String[] s =  {"A", "b", "C", "a", "c", "B"};

The result is

{"A", "B", "C", "a", "b", "c"};

Instead of

{"A", "a", "b", "B", "C", "c"}

Definitely sensitive stuff going on here. I’m running Processing version 1.0.9. So was the page just not updated since the last upgrade? Did a new version change old behavior of the library?

Writing for the Wastebasket

When I first saw on the Processing manual page that sort was case insensitive, I knew sorting ints and floats would not be an issue because I could call JavaScript’s built-in sort() method on an Array object. However, I knew I had to write my own string comparison function, so I did:

var strcmp = function(a, b)
{
  // assume they are equal
  var res = 0;

  // need to do case insensitive search
  var a_ = a.toUpperCase();
  var b_ = b.toUpperCase();

  var shortestLen = (a.length < b.length) ? a.length : b.length;

  for(var i=0;  i < shortestLen; i++)
  {
    res = a_.charCodeAt(i) - b_.charCodeAt(i);
    if(res != 0) break;
  }

  // if they are the same, but diff length, return shortest
  if(res == 0 && a.length != b.length)
  {
    res = (a.length < b.length) ? -1: 1;
  }

  return res;
}

It might not seem like a really big deal, but take into account looking up documentation pages and fixing bugs here and there. But it gets worse. For some ridiculous reason I thought I needed to implement my own sorting algorithm. Didn’t I see that sort() takes a comparison function? When I started, I wrote a little bubble sort which I planned to convert to a merge sort. So last week or so I got busy and wrote:

var mySort = function(arr)
{
  var isSorted = false;

  do
  {
    isSorted = true;
    for(var i = 0; i < arr.length-1; i++)
    {
      // wrong order, swap
      if(strcmp(arr[i], arr[i+1]) > 0)
      {
        var a = arr.splice(i,1);
        var b = arr.splice(i,1);

        // convert to string?
        arr.splice(i,0,""+b);
        arr.splice(i+1,0,""+a);
        isSorted = false;
      }
    }
  }while(isSorted == false);
}

I honestly don’t care about the code, but man, I just wasted a bunch of time. Or did I? I have no idea. But again, it gets worse. This morning I took on the challenge of finding a some JavaScript merge sort code. I crammed it into the Processing.js library. After about an hour, I fixed some stupid small bugs and had it working. I went back in to Processing’s PDE and saw Processing was now case sensitive! Had it changed since the last time I upgraded from 1.0.7 to 1.0.9!? Was it always like this? Did I just waste all my time when I could have been calling JavaScript’s built-in sort()?

This seems like such a silly issue, why in the world is it causing me such frustration? :(

The worst part is I can’t sort my thoughts because my sort() is all screwy.

Comments»

No comments yet — be the first.