AnsweredAssumed Answered

javascript sort function

Question asked by chrisokelly on Sep 5, 2012
Latest reply on Sep 6, 2012 by romschn
Hi guys,

I am developing a dashlet webscript to display tickets from RT in Alfresco. The dashlet is working fine, my next step is to add sorting to it. I have attempted to do so, but I get an error message:

Failed to execute script 'classpath*:alfresco/site-webscripts/org/alfresco/components/dashlets/showRTTickets.get.js': 08050000 TypeError: Cannot find function sort. (jar:file:/opt/alfresco-4.0.d/tomcat/shared/lib/showRTTickets.jar!/alfresco/site-webscripts/org/alfresco/components/dashlets/showRTTickets.get.js#7)

As far as I can see my syntax is fine, I'm pulling my hair out here. My sort function looks like:
function ticketSort(tickets, sortBy, sortType)
   {
   var result={};
   switch(sortBy)
      {
      case "id": //integer sort
      result = tickets.sort(function(a, b){return a.id-b.id;});
      break;
      case "queue": //string sort
      result = tickets.sort(function(a, b){
         var stringA=a.queue.toLowerCase(), stringB=b.queue.toLowerCase();
         if (stringA < stringB)
            return -1;
         if (stringA > stringB)
            return 1;
         return 0;
      });
      break;
      case "owner": //string sort
      result = tickets.sort(function(a, b){
         var stringA=a.owner.toLowerCase(), stringB=b.owner.toLowerCase();
         if (stringA < stringB)
            return -1;
         if (stringA > stringB)
            return 1;
         return 0;
      });
      break;
      case "creator": //string sort
      result = tickets.sort(function(a, b){
         var stringA=a.creator.toLowerCase(), stringB=b.creator.toLowerCase();
         if (stringA < stringB)
            return -1;
         if (stringA > stringB)
            return 1;
         return 0;
      });
      break;
      case "subject": //string sort
      result = tickets.sort(function(a, b){
         var stringA=a.subject.toLowerCase(), stringB=b.subject.toLowerCase();
         if (stringA < stringB)
            return -1;
         if (stringA > stringB)
            return 1;
         return 0;
      });
      break;
      case "status": //string sort
      result = tickets.sort(function(a, b){
         var stringA=a.status.toLowerCase(), stringB=b.status.toLowerCase();
         if (stringA < stringB)
            return -1;
         if (stringA > stringB)
            return 1;
         return 0;
      });
      break;
      case "priority": //integer sort
      result = tickets.sort(function(a, b){return a.priority-b.priority});
      break;
      case "created": //date sort
      result = tickets.sort(function(a,b) {
         var dateA=new Date(a.created), dateB=new Date(b.created);
         return dateA-dateB;
      });
      break;
      case "due": //date sort
      result = tickets.sort(function(a,b) {
         var dateA=new Date(a.due), dateB=new Date(b.due);
         return dateA-dateB;
      });
      break;
      case "lastupdated": //date sort
      result = tickets.sort(function(a,b) {
         var dateA=new Date(a.lastUpdated), dateB=new Date(b.lastUpdated);
         return dateA-dateB;
      });
      break;
      }
   if (sortType == "desc")
      return results.reverse();
   else
      return results;
   }

I get the impression that the sort function is missing in Rhino perhaps, similar to the xmlhttprequest function. I actually implemented the xmlhttprequest object by adding a jar file and a bean just last week, does anyone know of one for the sort function (My google-fu could not dig one up)? Or is there perhaps a function in the Alfresco API somewhere to provide a similar mechanism (all that I could find was search related)?

Outcomes