如何在 JsonResult 中按降序对数据进行排序?

How to sort data in Descending order in JsonResult?

我有这个功能

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);
    }

我在 ds.ReadXml 中获取值,然后当它加载到此处的 DataTable 中时,我想按降序排列或排序行。我在这样的两列中得到值:-

1 24/04/2017 26SI170424PH0001

2 24/04/2017 26SI170424PH0002

3 24/04/2017 26SI170424PH0003

我想按 "desc" 顺序排序。第一列按日期排列并使用第二列的最后 3 位数字。

此外,这是我的 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");
}

我设置了 row.Date 因为我不知道日期列名或标识符列名,我说的是日期和数字:

  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()

谢谢乔迪, 我找到了另一种方法来做同样的事情。

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

这也行。, 祝你有个愉快的一天。,