Sort overrides in javascript

Published 18 August 06 05:09 PM | Shankarr Kalyanaraaman 

For many a transition between languages with wonderful organization and amazing features to JavaScript which is language where you have lesser control over your code and memory and not as much sophistication is a harder experience. If "harder" is a strong word, the best may be "not a fuzzy feeling" as my codeveloper goes...

So, that being said, I have started to love this language, here is one of my first articles about this interesting language, which has essentially started to conquer the web....

Alright...prologue's over..to the coding zone now...

So, I know people have been using the sort function which comes handy with the array data type in javascript. But, here is the true power which javascript gives us.

overrides on the sort functionality!. Atleast I know for sure dotnet gives us the ability to have IComparers which need to be written when implementing Sort on custom data structures.  In similar lines javascript allows function to be passed which returns the results of comparison and using a vary proprietary logic for comparing two elements.

//Code start

function SortArray(unsortedArray, IComparerInstance)
{
 function ctor()
 {
  SortArray.refCount += 1;
  SortArray.steps = 0;
  doSort();
 }
 
 function doSort()
 {
  if(typeof unsortedArray == "object" && 
	unsortedArray && 
	unsortedArray.constructor == Array)
  {
   if(typeof IComparerInstance == "function")
   {
    unsortedArray.sort(IComparerInstance);
   }
   else
   {
    unsortedArray.sort();
   }
   
   document.write(ArrayToString(unsortedArray) + '
'); } } ctor(); } SortArray.refCount = 0; SortArray.steps = 0; function ArrayToString(inArray) { if(typeof inArray == "object" && inArray && inArray.constructor == Array) { var cStr = ""; for(var i = 0; i < inArray.length; i++) { cStr += i + " : " + inArray[i] + " "; } return cStr; } else { return null; } } function SortByStringLength (str1, str2) { if(typeof str1 != "string" || typeof str2 != "string") { throw('one of the arguments is not a valid type'); } var l1 = str1.length; var l2 = str2.length; SortArray.steps += 1; return (str2.length - str1.length); } function init() { var main = [ 'apple', 'orange', 'banana', 'papaya', 'jackfruit', 'honeydew', 'watermelon', 'strawberry', 'grape', 'lychee', 'tangerine', 'lemon', 'lime' ]; SortArray(main, SortByStringLength); //alerts section alert(SortArray.refCount); }; var resultEl = document.getElementById('results'); init();

//Code end

If you closely observe the code above, it has various features. The SortArray function is like a class with constructors (private || sealed) and the init function passes a function pointer which is called every time the array's sort method compares two elements. If you actually paste this code to a simple html file in a script tag, you can see that the strings inside the array are sorted in order of their length. Also note the comment which indicates changing of sort order from ascending to descending and vice versa.

I am going to stop this post right here and not bore all of you readers with contents that are pages long. So more on this in my next post....till then

- Shankarr

Comments

No Comments
Anonymous comments are disabled
Page view tracker