How to sort data in Descending order in JsonResult?

I have this function

public JsonResult GetStoreIndNo(string STranNo, string sStatus, string 
SToStore)
    {
        StoreMdClient smClient = new StoreMdClient();
        string StoreCode = ("" + HttpContext.Session["StoreCode"]).Trim();
        DataSet ds = new DataSet();
        if (sStatus == "N") ds.ReadXml(new XmlTextReader(new StringReader(smClient.GetStoreIndNoDataNew(SToStore, StoreCode, "", BL_GeneralFunctionsSUMDTLS.Summary))));
        else ds.ReadXml(new XmlTextReader(new StringReader(smClient.GetStoreIndNoDataDisply(STranNo, BL_GeneralFunctionsSUMDTLS.Summary))));

        DataTable load = new DataTable();
        if (ds.Tables.Count > 0) load = ds.Tables[0];
        var jsondata = new
        {
            rows = (from DataRow row in load.Rows
                    select new
                    {
                        id = ("" + row["INDENTNO"]).Trim(),
                        cell = new string[] { 
                            GeneralFunction.GetDate(("" + row["INDENTDATE"]).Trim()), ("" + row["INDENTNO"]).Trim()
                     }
        }).ToArray()
        };
        return Json(jsondata);
    }

I get value in ds.ReadXml after which when it is loaded in DataTable here, I want to arrange or sort the rows with descending order. I get values in two column like this :-

1 24/04/2017 26SI170424PH0001

2 24/04/2017 26SI170424PH0002

3 24/04/2017 26SI170424PH0003

I want to sort in "desc" order. Datewise in first column and using the last 3 digits of second column.

Also, here is my jQuery:

function StoreIndNoBind(condiA) {
        if (condiA == 'empty' || condiA == 'enablefill' || condiA == 'disablefill') {
            var FrmdateA = ""; var ToDateA = "";
            var sStatus = gact;
            var SToStore = ("" + $("#cmbToStoreSel").select2("val")).trim();
            if (condiA == 'empty') {
                STranNo = "";
                var urlq = "GetStoreIndNo?STranNo=" + STranNo + "&&sStatus=" + sStatus + "&&SToStore=" + SToStore;
            }
            else {
                STranNo = ("" + $("#txtTranNo").val()).trim();
                var urlq = "GetStoreIndNo?STranNo=" + STranNo + "&&Status=" + sStatus + "&&SToStore=" + SToStore;
            }
        }
        colnames = ['Indent Date', 'Indent Number'];
        $("#GridIndentNo").jqGrid('GridUnload');
        $("#GridIndentNo").clearGridData();
        $("#GridIndentNo").jqGrid({
            url: urlq,
            datatype: 'json',
            mtype: 'post',
            colNames: colnames,
            colModel: [
                { name: 'INDDATE', index: 'INDDATE', width: 93, editable: false },
                { name: 'INDENTNO', index: 'INDENTNO', width: 140, editable: false }
            ],
            sortname: 'id',
            sortorder: 'desc',
            shrinkToFit: false,
            hidegrid: false,
            gridView: true,
            rownumbers: true,
            loadonce: true,
            multiselect: true,
            sortableColumns: false,
            cellsubmit: 'clientArray',
            height: 110,
            width: 316,
            rowNum: 9999999999,
            ondblClickRow: true,
            loadtext: 'Loading.....',
            footerrow: false,
            gridComplete: function () {
                if (condiA != 'empty' && condiA != '') {
                    var ids = jQuery("#GridIndentNo").jqGrid('getDataIDs');
                    if (ids.length >= 0) {
                        for (var i = 0; i < ids.length; i++) {
                            $(this).find('>tbody>tr.jqgrow>td>input[type="checkbox"]')[i].checked = true;
                        }
                        if (condiA == 'disablefill') {
                            var cbs = $("tr.jqgrow > td > input.cbox", $("#GridIndentNo")[0]);
                            var hbs = $("tr.ui-jqgrid-labels > th > div > input.cbox", $("#GridIndentNo")[0]);
                            cbs.prop("disabled", "disabled");
                            hbs.prop("disabled", "disabled");
                        }
                    }
                }
            },
            beforeSelectRow: function (rowid, e) {
                if (e.target.type == 'checkbox' && e.toElement.cellIndex == undefined) {
                    if (e.target.checked == true) GetIndentMtrlBind(rowid,e);
                    else RmvMtrlForIndentNo(rowid);
                }
            },
        }).trigger("reloadGrid");
        $("#cb_GridIndentNo")[0].style.display = 'none';
        $('.ui-jqgrid-sdiv').removeClass('ui-widget-content');
        $("#GridIndentNo").parents('div.ui-jqgrid-bdiv').css("overflow-y", "scroll");
}

2 answers

  • answered 2018-05-16 07:46 Jordi Jordi

    I've set row.Date because i dont know the Date column name or the identifier column name, i've said them Date and Number:

      rows = (from DataRow row in load.Rows
                  let lasts = row.Number.Substring(row.Number- 3, 3)
                 orderby row.Date descending, lasts descending
                    select new
                    {
                        id = ("" + row["INDENTNO"]).Trim(),
                        cell = new string[] { 
                            GeneralFunction.GetDate(("" + row["INDENTDATE"]).Trim()), ("" + row["INDENTNO"]).Trim()
                     }
        }).ToArray()
    

  • answered 2018-05-16 09:20 Abhiro

    Thanks Jordi, I have found yet another way to do the same.

     DataView dv = ds.Tables[0].DefaultView;
     dv.Sort = "INDENTDATE desc";
     dv.Sort = "INDENTNO desc";
     DataTable sortedDT = dv.ToTable();
    

    this works too., Have a nice day.,