cancel
Showing results for 
Search instead for 
Did you mean: 

how to sort date in ui5 table?

Former Member
0 Kudos

hi,

i have used datepicker which is binded with json model and displayed with table.
i wanted to sort the dates,is there any method to do it>

?


thanks and regards.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

You can sort it as strings as in the following example:


        // override standard table sorter

        oTable.attachSort(function(oEvent) {

        

           var sPath = oEvent.getParameter("column").getSortProperty();

            var bDescending = false;

            if (oEvent.getParameter("sortOrder") == "Descending") {

                bDescending = true;

            }

           var oSorter = new sap.ui.model.Sorter(sPath, bDescending );

           

            // override compare function only for birthday path

           

            if (sPath === "birthday") {

                oSorter.fnCompare = function(a, b) {

               

                    // parse to Date object

                    var aDate = new Date(a);

                    var bDate = new Date(b);

                   

                    if (bDate == null) {

                        return -1;

                    }

                    if (aDate == null) {

                        return 1;

                    }

                    if (aDate < bDate) {

                        return -1;

                    }

                    if (aDate > bDate) {

                        return 1;

                    }

                    return 0;

                };

            }

          

           oTable.getBinding('rows').sort(oSorter);

            // prevent internal sorting by table

           oEvent.preventDefault();

        });

This example was taken from Snippix: http://veui5infra.dhcp.wdf.sap.corp:8080/snippix/#7887

but I attached it just in case you can't access it.

Hope that helps,

Ran

Former Member
0 Kudos

hi,

i cannot access the link.
but in your code i found certain doubts,
"birthday" would be my column name which is binded?

and in function compare what does a and b signify?

Former Member
0 Kudos

Hi

birthday would indicate the path in the model that's relevant to your date.

a & b are the parameters provided to the compare function for determining the order between elements. You don't need to provide these parameters anywhere, this method is called using a sorting algorithm of the table.

Ran

Former Member
0 Kudos

hi

i tried the code ,but it doesn't override the sort property and also the fields aren't sorted.
what could be the error.
?

also i have just binded static temporary string as date,so is this possible that it doesnt function on string as date.?

thanks and regards.

kartik

Former Member
0 Kudos

Hi Kartik,

I uploaded the mentioned sample to JSBin, I guess that if you'll see the working code it will answer your questions: JS Bin - Collaborative JavaScript Debugging&lt;/title&gt; &lt;link rel=&quot;icon&quot; href=&quot;h...

Regards,

Ran

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

Could you please tell if I change the pattern of date as dd.MM.yyyy then it is not working.What can be the possible solution.

Thanks