How to sort data on something other than displayed string?

How to sort data on something other than displayed string?

ChrisMiamiChrisMiami Posts: 7Questions: 3Answers: 0

I have 2 fields, a DateTime and a Time/Duration field, that are sorted incorrectly and I'd like to hope that there's a way to sort them according either to the original data or a reverse-transform (parse) on the display.

The date field shows date data like "Wed, Jan 4th, 9:48:01 AM", and is initialized as follows:

            { 'mData' : 'RowDateTime' 
             ,'render' : function(rawDate) {
                return moment(rawDate).format("ddd, MMM Do, h:mm:ss A");
             }
            },

The duration field displays a digital clock like "02:23:33", and is initialized as follows:

            { 'mData' : 'Duration' 
             ,'render' : function(seconds) {
                 var duration = moment.duration(Number(seconds), "seconds");
                 var durst = seconds;
                 if( seconds > 60 * 60 )
                   durst = padZero(duration.hours()  ) + ":" +
                           padZero(duration.minutes()) + ":" + 
                           padZero(duration.seconds());  
                 else                                
                   durst = padZero(duration.minutes()) + ":" + 
                           padZero(duration.seconds());  
                           
                 return "<div id='ralign'>"+durst+"</div>";
             }

Of course, the dates sort based on the first char of the day name, so Thu always comes before Wed.
The duration sorts left to right, so 7 hours (07:22:23) is sorted next to 7 minutes (07:12) and before 08:03!

How can I change these sorts in the easiest possible way? The first thing I'm going to try is add "'sType' : 'Date'" to my init.

Thanks,
Chris

Answers

  • ChrisMiamiChrisMiami Posts: 7Questions: 3Answers: 0

    Well, in case someone in the future needs this info (sorta sad that there are zero replies after 4 months), the solution is to accept the 'type' string argument in the render function and to use this to decide whether or not to format the data. You can find exhausting documentation on this page.

                { 'mData' : 'Duration' 
                 ,'render' : function(seconds, type) {
                   var orthogonalData = seconds;  
                   
                   if( type === 'display' || type === 'filter' ) {
                     var duration = moment.duration(Number(seconds), "seconds");
                     var durst = seconds;
                     if( seconds > 60 * 60 )
                       durst = padZero(duration.hours()  ) + ":" +
                               padZero(duration.minutes()) + ":" + 
                               padZero(duration.seconds());  
                     else                                
                       durst = padZero(duration.minutes()) + ":" + 
                               padZero(duration.seconds());  
                               
                     orthogonalData = "<div id='ralign'>"+durst+"</div>";
                   }
             
                   return orthogonalData;
                 }
                },
    

    Thus, if render type is 'display' or 'filter', we return the formatted duration string, otherwise we return raw data, so sorting works.

This discussion has been closed.