Base Classes Extensions and Bubble Sort with the Microsoft AJAX Library

Here are the original dates:

Here are the sorted dates:

Exercise Explanations

This is a OOP with Microsoft AJAX Library exercise from Chapter 5 of Microsoft AJAX Library Essentials: JavaScript in ASP.NET AJAX 1.0 Explained.

The Microsoft AJAX Library base classes are: Array, Boolean, Date, Error, Number, Object, and String. For detailed reference, apart from Appendix A, we recommend that you check the cheat sheets published at http://aspnetresources.com/: Microsoft AJAX Library Cheat Sheet Batch 1 and Microsoft AJAX Library Cheat Sheet Batch 2.

You create objects of a base class the same way you create other kinds of objects. For example, here's how you create an array of three string elements:

var myArray = new Array("one", "two", "three");

JavaScript's loose typing, although admittedly feeling a bit unnatural to many .NET programmers, has its advantages when it comes to coding fl exibility. For example, you can create arrays containing objects of any type. Moreover, each array element can be of any type. The fl ip side is that you need to be careful when the data type is not clear. For example, take this array:

var myArray = new Array("12/23/1980");

Although it may look like a date, this is an array that contains one string. Trying to use myArray[0] as a Date will have unexpected results because Date-specific methods aren't recognized by strings, and those that are have different functionality. To store the date as a Date, you would need to do something like this:

var myDate = Date.parseInvariant("1980/12/23", "yyyy/MM/dd");
var myArray = new Array(myDate);

This time, the array contains a Date object, thus inheriting all functionality provided by the Date type.

To demonstrate some typical coding involving base classes extensions, we'll go thourgh a simple example where we use the Bubble Sort algorithm to sort an array of dates, in ascending order of the date.

Bubble Sort is one of the simplest sorting algorithms to implement. It involves parsing the list of elements multiple times, comparing each element with the one that comes after it, and swapping their values if needed. The process is repeated until no swaps happen when parsing the entire list.

Note that Bubble Sort isn't one of the most efficient sorting algorithms, but we're using it in this exercise for its simplicity. If you're not familiar with this algorithm, we recommend that you read its description at WikiPedia page on Bubble Sort.

This little exercise demonstrates a few relevant ways of working with Microsoft AJAX Framework's base classes extensions.

Implement the exercise step by step and find detailed explanations in our book, Microsoft AJAX Library Essentials: JavaScript in ASP.NET AJAX 1.0 Explained.