应该如何对月份进行排序?

时间:2019-05-08 18:48:09

标签: javascript canvas

我正在使用javascript html5画布构建图表,并且试图按月对数据进行排序,但是当我输入它们时,它们将按字母顺序进行排序,而不是正确的月份顺序。

class RaceChart {
  constructor(canvas_id, data) {
    this.canvas = document.getElementById(canvas_id);
    this.data = data;

    this.first_time = true;
    this.date = null;
    this.date_data = null;
    this.id_to_left_text = {};
    this.id_to_left_image = {};
    this.id_to_center_text = {};
    this.id_to_center_image = {};
    this.id_to_bar_colors = {};
    this.value_anims = {};
    this.y_anims = {};
    this.label_anims = {};

    this.ctx = this.canvas.getContext("2d");
    request_animation_loop(() => this.draw());
  }

  show(date) {
    this.date = date;
    this.date_data = this.data.filter(row => row.date === date);
    this.date_data.sort((a, b) => b.value - a.value);
    for (let row of this.date_data) {
      this.id_to_bar_colors[row.id] = row.bar_color;
      this.id_to_left_image[row.id] = row._left_image;
      this.id_to_left_text[row.id] = row.left_text;
      this.id_to_center_image[row.id] = row._center_image;
      this.id_to_center_text[row.id] = row.center_text;
    }
  }

1 个答案:

答案 0 :(得分:1)

添加另一个只是纯数字0-11的参数。根据其文本月份值为每个对象分配一个数字。然后对这些数字进行排序