在多级对象中排序日期

时间:2017-04-05 08:57:51

标签: jquery sorting date object

我需要在对象中排序Date(作为键)。

我认为我应该在一个对象数组中转换它,然后我必须将它转换为一个对象,但它似乎太难了。这是多级对象的数据:

{
  "2017-02-18": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 13.4,
      "provider": 13.04
    }
  },
  "2017-02-14": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 5.4,
      "provider": 5.04
    }
  },
  "2017-02-13": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 1.4,
      "provider": 1.04
    }
  },
  "2017-02-17": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 21.4,
      "provider": 21.04
    }
  },
  "2017-02-15": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 44.4,
      "provider": 44.04
    }
  },
  "2017-02-16": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 56.4,
      "provider": 56.04
    }
  }
}

排序后,我需要一个具有此结果的对象:

{
  "2017-02-13": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 1.4,
      "provider": 1.04
    }
  },

  "2017-02-14": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 5.4,
      "provider": 5.04
    }
  },
  "2017-02-15": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 44.4,
      "provider": 44.04
    }
  },
  "2017-02-16": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 56.4,
      "provider": 56.04
    }
  },
  "2017-02-17": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 21.4,
      "provider": 21.04
    }
  },
  "2017-02-18": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 13.4,
      "provider": 13.04
    }
  }
}

感谢所有人的帮助!

1 个答案:

答案 0 :(得分:0)

引用this topic,我添加了日期排序功能:

var datesObj = {
  "2017-02-18": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 13.4,
      "provider": 13.04
    }
  },
  "2017-02-14": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 5.4,
      "provider": 5.04
    }
  },
  "2017-02-13": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 1.4,
      "provider": 1.04
    }
  },
  "2017-02-17": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 21.4,
      "provider": 21.04
    }
  },
  "2017-02-15": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 44.4,
      "provider": 44.04
    }
  },
  "2017-02-16": {
    "prod_work": {
      "product": 0,
      "product_type": 0
    },
    "prod_work1": {
      "tax": 56.4,
      "provider": 56.04
    }
  }
};

var datesArr =[];
for(var dateKey in datesObj)
  datesArr.push(dateKey);  
  
datesArr.sort(function(dt1, dt2){
        return (new Date(dt1)).getTime() -(new Date(dt2)).getTime();
   });
   
   for (i = 0; i < datesArr.length; i++) {
  
         alert(datesArr[i] + ':' + datesObj[datesArr[i]]);
}