How to sort a child table

My parent doctype name is student with fields name1(data) and passport_details(Table).
Child table name is StudentPassport with fields passport_number, issue_date and expiry_date. I want to sort the child table in ASC/DESC order based on expiry_date. I tried with the below client script

frappe.ui.form.on('Student', {
	 refresh: function(frm) {
//console 1
		console.log("child");
		frm.fields_dict['passport_details'].grid.get_field('expiry_date').get_query = function(doc, cdt, cdn) {
			var child = locals[cdt][cdn];
//console 2
			console.log(child);
			return {   
				order_by: 'expiry_date ASC'				
			};
		};
	 }
});

No 2nd console output and not able to perform sort in the child table. Also In Customize Form - StudentPassport, I set the sort field as expiry_date and sort order as ASC in list settings but not worked.

Hmm @madhusona,

I haven’t any idea and I’m not used to order_by for this type of script but try it.

frappe.ui.form.on('Student', {
    refresh: function(frm) {
        frm.fields_dict['passport_details'].grid.get_field('expiry_date').get_query = function(doc, cdt, cdn) {
            return {
                order_by: 'expiry_date ASC',
            };
        };
    }
});

Thank You!

@NCP Already, I tried, but its not working. I have not seen any difference in your solution

I apologize for that. If the client script is not working as expected then apply with server script with some logic.

Again sorry @madhusona.

Thank You!

The following Client Script worked. Anyway thanks @NCP

var sortedEntries = frm.doc.passport_details.sort(function (a, b) {
        var dateA = new Date(a.expiry_date);
        var dateB = new Date(b.expiry_date);
        return dateB - dateA;
    });

    frm.set_value('passport_details', sortedEntries);
    frm.refresh_field('passport_details');

Reference:

  1. Server Script - Get Nearest Date from Child Table? - #9 by pmjd