Javascript以反向时间顺序排序对象数组

时间:2015-06-22 04:01:49

标签: javascript arrays sorting

我有一个包含作业列表的对象数组,我想按照时间顺序对它们进行排序,就像它们出现在简历上一样。 我提出了以下解决方案,似乎'工作,但我想知道是否有更好的方法。

我从服务器返回的数据采用以下格式。

	if(searchedArray) {
                searchedArray.forEach(function (row) {
                buildData.push({r:rowCount,w:row['w'],n:'1',nl:'2',o:row['o'],t:row['t'],d:row['d'];
                    rowCount++;
                });
            }
            var searchData = new kendo.data.DataSource({data: buildData});


	var sGrid=null;
	
	sGrid = $("#searchedgrid").kendoGrid({
		        toolbar: ["excel"],
		        excel: {
		            fileName: "filename.xlsx",
		            proxyURL: "http://demos.telerik.com/kendo-ui/service/export",
		            filterable: true
		        },
		        dataSource: searchData,
		        sortable: {
		            mode: "multiple",
		            allowUnsort: true
		        },
		        schema: {
		            model: {
		                fields: {
		                    r: { type: "number" },
		                    w: { type: "number" },
		                    n: { type: "string" },
		                    nl: { type: "string" },
		                    o: { type: "string" },
		                    t: { type: "string" },
		                    d: { type: "date" }
		                }
		            }
                },
                height: sHeight,
                scrollable: true,
                pageable: false,
		selectable: "multiple cell",
                allowCopy: true,
                columns: [
                    { field: "r",width: 40,title: "Rank",template:'<center>#=r#</center>'},
                    { field: "w",width: 50,title: "Weight",template:'<center>#=w#</center>'},
                    { field: "n", title: "Number", width: "80px",template:'<center>#=n#</center>'},
                    { field: "nl", title: "&nbsp;", width: "14px",template:'<center><a href="#=nl#" onclick="javascript:void window.open(\'#=nl#\',\'details\',\'width=800,height=600,toolbar=0,menubar=0,location=0,status=1,scrollbars=1,resizable=1,left=0,top=0\');return false;"><div class="ambLink" id="detaillink" title="View details"></div></a></center>',sortable:false},
		    { field: "o",width: 200,title: "Owner"},
                    { field: "t",width: 400,title: "Title",  attributes: {style: 'white-space: nowrap '} },
                    { field: "d",width: 70,title: "Filed",template:'<center>#=d#</center>'}
                ]
            }).data("kendoGrid");  

	$("#searchedgrid").data('kendoGrid').refresh();

我想出了为每个项目添加时间顺序哈希值的函数。

// ms=> monthStart me=>monthEnd etc...
var jobs = [
    {
        ms: 06,
        ys: 2013,
        me: 02,
        ye: 2015
    },
    {
        ms: 09,
        ys: 2013,
        me: 02,
        ye: 2014
    },
    {
        ms: 08,
        ys: 2000,
        me: 06,
        ye: 2016
    },
    {
        ms: 01,
        ys: 2014,
        me: 02,
        ye: 2017
    }
];

1 个答案:

答案 0 :(得分:2)

正如PM 77-1建议的那样,考虑将内置Array.prototype.sort与Date对象一起使用。大概你想要在开始或结束时对它们进行排序:

{{1}}