-
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
-
This is really cool... I mean slick. Check out the recently released windows live writer beta. You do not have to log on to the website (it stores the credentials for you and all you have to do to post a blog entry open your live writer and put your content and click publish....
how cool is that ? experience it...
-
As one of the developers behind local.live.com (specifically the collections feature) , it is such a great pleasure for me to spread the word about the new local.live.com. There is a great deal of new stuff in it this time.
We have a completely new look and feel for the system and amazing features like the Collections, Traffic, a brand new Driving directions etc.
What more ? check out www.passthepoi.com. You can create great collections and make them public and post it on www.passthepoi.com and be a part of the wave.
Also check out http://blogs.msdn.com/chota
Check it out....enjoy !!!