Alessandro Lacava’s Blog

Google
 

December 7, 2006

The Productivity Perks Behind Prototype’s Popularity

Filed under: Computer, JavaScript, AJAX, Prototype — alessandrolacava @ 12:38 pm

Learn how to leverage the popular Prototype JavaScript framework to speed up your AJAX-based development. You’ll see how to use JavaScript in an object-oriented way.

Click Here to read the full article.


November 20, 2006

Sorting an array of objects with the sortBy method of Prototype

Filed under: Computer, JavaScript, Prototype — alessandrolacava @ 5:34 pm

Here is an example of how easy it is to sort an array of objects using the Enumerable.sortBy method of the Prototype framework:

JavaScript:
  1. var obj1 = {
  2. lastName : "Lacava",
  3. firstName : "Alessandro"
  4. };
  5.  
  6. var obj2 = {
  7. lastName : "Brown",
  8. firstName : "John"
  9. };
  10.  
  11. var obj3 = {
  12. lastName : "Simpson",
  13. firstName : "Bart"
  14. };
  15.  
  16. var arr = [obj1, obj2, obj3];
  17.  
  18. //order by last name
  19. var sorted = arr.sortBy( function(obj)
  20. {
  21. return obj.lastName.toLowerCase();
  22. }
  23. );
  24.  
  25. var str = "";
  26. sorted.each( function(obj, index)
  27. {
  28. str += index + 1 + " - " + obj.lastName + " " + obj.firstName + "
  29. ";
  30. }
  31. );
  32.  
  33. //display the elements ordered by last name
  34. document.write(str);