Custom Sorting of filtered options of Link Data Field via client script

I am setting filter on link field via client script frm.set_query. This is working fine. Now I want to sort this filtered list based on one of the data field of the link field; is there a way to do this?

I have tried adding order by but not working
frm.set_query(“custom_item_packing_instruction”, “items”, function (doc, cdt, cdn) {
return {
“filters”: {
“disabled”: 0,
“item”: row.item_code
},
“order_by”: “conversion_factor”,
};
});

That for, you have to use the custom query method to solve your issue.

Doc: Overriding Link Query By Custom Script

Custom script seems to be written in python. Any options for doing this on client side script (Java Script)

No, you have to use client and server both side.

Example:

frappe.ui.form.on('Sales Invoice', {
    setup: function (frm) {
        frm.set_query("custom_item_packing_instruction", "items", function (doc, cdt, cdn) {
            let row = frappe.get_doc(cdt, cdn);
            return {
                query: "your_custom_app_name.path.to.method.get_sorted_items",
                filters: {
                    item: row.item_code
                }
            };
        });
    }
});
@frappe.whitelist()
def get_sorted_items(doctype, txt, searchfield, start, page_len, filters):
    item_code = filters.get("item")
    return frappe.db.sql("""
        SELECT name 
        FROM `tabCustom Item Packing Instruction` 
        WHERE disabled = 0 AND item = %s
        ORDER BY conversion_factor ASC
    """, (item_code))

Can the server script be defined DocType “Server Script”?

No, you have to build a custom app for that.

Thank you for your detailed replies. To the point and accurate; Very helpful!!