Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 对我来说,以排序的方式检索这些信息的最佳方式是什么?_Javascript_Algorithm - Fatal编程技术网

Javascript 对我来说,以排序的方式检索这些信息的最佳方式是什么?

Javascript 对我来说,以排序的方式检索这些信息的最佳方式是什么?,javascript,algorithm,Javascript,Algorithm,情况:我有一段代码 var DealsByMonth = {}; for (var region in this.DealsByRegion) { this.DealsByRegion[region].forEach(function (deal) { // deal.Created is in form like "2015-05-04T08:26:38Z" var parts = deal.Created.split('-'),

情况:我有一段代码

var DealsByMonth = {};
for (var region in this.DealsByRegion) {
    this.DealsByRegion[region].forEach(function (deal) {
        //  deal.Created is in form like "2015-05-04T08:26:38Z"
        var parts = deal.Created.split('-'),
            monthstr = [parseInt(parts[1]), parseInt(parts[0])].join("/");
        if (DealsByMonth[monthstr] !== undefined) {
            ++DealsByMonth[monthstr]
        } else {
            DealsByMonth[monthstr] = 0;
        }
    });
}
console.log(DealsByMonth); // TEST

 var line_data = {
    labels: [],
    datasets: [
        {
            label: "MSfC Deals - Month by Month",
            fillColor: "rgba(220,220,220,0.2)",
            strokeColor: "rgba(220,220,220,1)",
            pointColor: "rgba(220,220,220,1)",
            pointStrokeColor: "#fff",
            pointHighlightFill: "#fff",
            pointHighlightStroke: "rgba(220,220,220,1)",
            data: []
        }
    ]
 }; 

 for ( var key in DealsByMonth ) 
 {
    line_data.labels.push(key);
    line_data.data.push(DealsByMonth[key]);
 }
正在打印的是一个类似于

{1/2015: 6, 2/2015: 14, 3/2015: 15, 4/2015: 24, 5/2015: 33, 6/2015: 16, 7/2015: 14, 8/2015: 22, 9/2015: 29, 10/2014: 41, 11/2014: 9, 11/2015: 14, 12/2014: 1, 12/2015: 32}
我需要从该对象中提取的是关键点,但我需要按顺序遍历它们,因为我正在使用它们绘制线图。要么我需要按顺序检查它们,要么我需要重做代码,使它们处于已经按顺序排列的数据结构中


从优雅、效率、可读性等方面来说,正确的方法是什么?

在我的脑海中,对键进行排序,然后使用排序的顺序

Object.keys(DealsByMonth).sort(function(a, b) {
    var sa = a.split('/'); // index 0 = month, 1 = year
    var sb = b.split('/');
    var index = (sa[1] == sb[1]) ? 0 : 1; // if year is same, compare months
    return parseFloat(sa[index]) - parseFloat(sb[index]);
}).forEach(function(key) {
    line_data.labels.push(key);
    line_data.data.push(DealsByMonth[key]);
});
类似于

multiSort(DealsByMonth);
函数多重排序(对象){
变量月份=Object.keys(对象);
月份。排序(功能(a、b){
var m1=a.split('/')[0];
var m2=b.split('/')[0];
变量y1=a.split('/')[1];
变量y2=b.split('/')[1];
如果(y1y2)返回1;
if(parseInt(m1)parseInt(m2))返回1;
返回0;
});
var reorderdobj={};
对于(变量i=0;i
如果希望数据在对象中正确排序,则应以正确比较键的方式保存键(您希望的方式)。当比较两个日期时,首先要比较的是它们的年份,然后是月份,然后是天数。在您的代码中,您保留了一年前一个月的代码,例如:
“1/2015”
,如果您希望正确订购,则这是不正确的,因为基于此格式,
“4/2014”
位于
“1/2015”
之后,这不是您想要的

因此,我建议您将年份放在前面,并将月份放在后面,更改:

monthstr = [parseInt(parts[1]), parseInt(parts[0])].join("/");
致:


我会将它们重新排序为YYYY/MM格式
monthstr = [parseInt(parts[1]), parseInt(parts[0])].join("/");
monthstr = [parseInt(parts[0]), parseInt(parts[1])].join("/");