Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
JavaScript
LoadLeadCourseRecentActivityList: function () {
       var leadCourseId = $("#hdnLeadCourseId").val();
       var args = {
           leadCourseId: leadCourseId
       }
       $.ajax({
           url: "/Lead/LoadLeadCourseActivityList",
           dataType: "json",
           type: 'GET',
           data: args,
           cache: false,
           async: false,
           success: function (data) {
               var leadCourseActivities = '<table id="tblLeadCourseRecentActivities" class="table table-striped table-bordered table-hover table-responsive"><thead><tr class="primary"><th>Activity Type</th><th>Subject</th><th>Description</th><th>Due On</th></tr></thead><tbody>';
               var countRA = 1;
               debugger;
               data = data.sort((function (a, b) {
               if (true)return (a[data.DueDate] > b[data.DueDate]) ? -1 : ((a[data.DueDate] < b[data.DueDate]) ? 0 : 1);
               //else return (b[data.DueDate] > a[data.DueDate]) ? -1 : ((b[data.DueDate] < a[data.DueDate]) ? 1 : -1);
               }));

               //data = sortByKey(data, 'data.DueDate');
               //data = sortfun(data, 'data.DueDate');

               $.each(data, function (i, CourseActivity) {
                   if (CourseActivity.ActivityTypeCode == null)
                       CourseActivity.ActivityTypeCode = "";
                   if (CourseActivity.Subject == null)
                       CourseActivity.Subject = "";
                   if (CourseActivity.Description == null)
                       CourseActivity.Description = "";
                   if (CourseActivity.DueDate == null)
                       CourseActivity.DueDate = "";

                   if (countRA <= 5) {
                       leadCourseActivities += "<tr><td>" + CourseActivity.ActivityTypeCode + "</td><td>" + CourseActivity.Subject + "</td><td>" + StringConcate(CourseActivity.Description) + "</td><td>"
                           + $.format.date(CourseActivity.DueDate, "dd-MMM-yyyy hh:mm a") + "</td></tr>";
                   }
                   countRA++;
               });
               leadCourseActivities += "</tbody></table>";
               $('#dvRecentActivity').html(leadCourseActivities);
           },
           error: function () {
               alert('Error while loading lead course activities');
           }
       });
   }
Posted
Comments
dan!sh 29-Jan-16 1:33am    
Your service can return the data sorted. Can't it?
Sinisa Hajnal 29-Jan-16 7:20am    
You cannot sort JSON, it is just text. What you *can* do is sort items returned, either in data source before writing to JSON or before showing them in the control.
Nathan Minier 29-Jan-16 12:00pm    
I haven't used it in a bit, but I'm pretty sure that jQuery .ajax() parses the JSON automatically when you set it as the dataType.

1 solution

I have a little cheater method that I use for sorting by property in JavaScript. I'm feeling magnanimous, so you can have it today, in all its scary-long glory.

JavaScript
propSort = function(array,prop, desc) {
    array.sort(function(a, b) {
        if (a[prop] < b[prop])
            return desc ? 1 : -1;
        if (a[prop] > b[prop])
            return desc ? -1 : 1;
        return 0;
    });
}


This is the basic JavaScript array sort custom filter pattern. It uses basic JS comparison to make determinations, which is always shocks me in terms of accuracy, coming from a strongly-typed language world.

JavaScript
...
success: function (data) {
    var leadCourseActivities = '<table id="tblLeadCourseRecentActivities" class="table table-striped table-bordered table-hover table-responsive"><thead><table><tbody><tr class="primary"><th>Activity Type</th><th>Subject</th><th>Description</th><th>Due On</th></tr></tbody></table></thead><tbody>';
    var countRA = 1;
    debugger;
    propSort(data,'DueDate',true);
...
</tbody></table>


The array.sort function works on the array itself, it doesn't spin off a copy. It's basically like working with a reference in a C-derivative language.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900