按字典中的枚举值对字典进行角度排序的数组

时间:2018-07-04 10:18:17

标签: javascript angular sorting dictionary enums

我有一些从http服务调用的数据。此数据包含月份名称,这些名称按自然日历顺序定义为枚举对象...现在,我有一个数组,在其中将月份名称与一些其他整数一起推到字典中。例如:

[{name:Februar, value: 999},{name: Januar, value: 123}, {name: Oktober, value: 321},...]

然后我将字典推入数组。现在,我想按字典枚举条目对数组进行排序...因此,一月份到来,然后是二月...,等等。

[{name: Januar, value: 123},{name:Februar, value: 999}, {name: Oktober, value: 321},...]

我的代码如下:

import { Component, OnInit, NgModule } from '@angular/core';
import {Http,Response} from '@angular/http';
import 'rxjs/add/operator/map';
import { BrowserModule } from '@angular/platform-browser';
import { NgxChartsModule } from '@swimlane/ngx-charts';

@Component({
  selector: 'productsbydemomonth',
  templateUrl: './productsbydemomonth.component.html',
  styleUrls: ['./productsbydemomonth.component.scss']
})
export class ProductsbydemomonthComponent implements OnInit {

  dataArray : any[];
  Anzahl: number[] = [];
  Monat: Monat;
  chartData: any = [];

  view: any[] = [700, 400];

  // options
  showXAxis = true;
  showYAxis = true;
  gradient = false;
  showLegend = true;
  showXAxisLabel = true;
  xAxisLabel = 'Monat';
  showYAxisLabel = true;
  yAxisLabel = 'Anzahl';


  monatConst: number = 12;

  colorScheme = {
    domain: ['#5AA454', '#A10A28', '#C7B42C', '#AAAAAA']
  };
  monthArray: Monat[] = [Monat.Januar, Monat.Februar, Monat.Maerz, Monat.April, Monat.Mai, Monat.Juni, Monat.Juli, Monat.August, Monat.September, Monat.Oktober, Monat.November,Monat.Dezember ]

  constructor(private http: Http) { }

  ngOnInit() {
    //this.getChartsData();
  }

  getChartsData() {
    return this.http.get('http://localhost:5555/DNZ/Produkt/Monat/by/' + (this.monatConst))
    .subscribe((res: Response) => {
      this.chartData = res.json() ;
      this.dataArray = this.formatData(this.chartData);       
    },
    (err) => {
      if (err.error instanceof Error) {
        console.log('Client-side error occured.');
      } else {
        console.log('Server-side error occured.');
      }
    });
  }

  formatData(someData) {
    this.dataArray = [];

    for (let dat of someData) {
      var dictChart = {
        name: dat.Monat,
        value: dat.Anzahl,      
      };
      //console.log(dictChart);
      this.dataArray.push(dictChart);
    }
    //console.log("Im Array:", this.dataArray);
    this.dataArray.sort(name);
    return this.dataArray;
  }

}


interface chartData {
  Anzahl : number,
  DemoID : number,
  Monat: Monat
}

enum Monat {"Januar","Februar","Maerz","April","Mai","Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"}

如何将字典中的数据按月份名称排序?以便它们在X轴上以正确的月份名称顺序显示在图表中? (不必理会图表,它只会显示数组顺序...)

3 个答案:

答案 0 :(得分:0)

将月份映射到要排序的值

您要使用.sort,但sort使用值进行上下排序,因此,您必须每月将其映射为如下所示的值:

const months = {
	January: 0,
	February: 1,
	March: 2,
	April: 3
}

let yourObject = [{month:'April'},{month:'January'},{month:'March'}]

console.log(yourObject.sort((data, prev) => months[data.month] - months[prev.month]))

.sort((data, prev) => months[data.month] - months[prev.month])

答案 1 :(得分:0)

sort方法需要比较器函数作为参数,例如:

const monthNum = month => this.monthArray.indexOf(month);
dataArray.sort((a, b) => monthNum(a) - monthNum(b))

基本上,该函数接受两个参数,并且应返回小于0,等于0或大于0的值(顺便说一下,truefalse分别等于1和0,因此它可能实际上也返回布尔值)。

答案 2 :(得分:0)

您可以尝试以下解决方案:

arr.sort((data, prev) => {
    return this.monthArray.indexOf(data.name) - this.monthArray.indexOf(prev.name);
})