简体   繁体   中英

How do I sort the data in ascending order from a JSON and output it to table?

I work with mat-table and used GET to output my data. Now I would like to sort the data ascending by the db-nr from my JSON.

Extract from my JSON:

{
                    "period": 12.0,
                    "amount": 0.0,
                    "name": "Test1",
                    "mandantKagId": 0.0,
                    "db-nr": 5
                },
                {
                    "period": 12.0,
                    "amount": 0.0,
                    "name": "Test2",
                    "mandantKagId": 0.0,
                    "db-nr": 2
                },
                {
                    "period": 12.0,
                    "amount": 0.0,
                    "name": "Test3",
                    "mandantKagId": 0.0,
                    "db-nr": 4
                }

// TS

// Fill the table with data
  private loadData() {
this.planBwaService.getAllPlanBwaData(yearString).subscribe(
          (resp: any) => {
            const planBwa = resp.success ? resp.bwaResult.planBwa : null;
            const data = planBwa['0'];
            const allYearlyResults = planBwa.yearlyResults;
            const yearlyResults = allYearlyResults['0'];
            if (null !== data && data && yearlyResults && yearlyResults !== null) {
              this.isLoading = false;
              const rows = [];
              const planBwaData: string[] = [];
              const names = _.uniq(data.map((d) => d.name)) as string[];
              for (const n of names) {
                planBwaData[n] = data.filter((d) => d.name === n);
                if (planBwaData[n].length > 0) {
                  const row: any = { name: planBwaData[n][0].name, values: {}, note: '', mandantKagId: planBwaData[n][0].mandantKagId };
                  for (const item of planBwaData[n]) {
                    row[item.period] = this.transformAmount(item.amount);
                  }
                  const yrNameItem = yearlyResults.find((yr) => yr.name === n);
                  row.values.currentYear = (yrNameItem && yrNameItem !== null) ? this.transformAmount(yrNameItem.amount) : null;
                  rows.push(row);
                }
              }
              this.dataSource = new MatTableDataSource<PlanBwa>(rows);
              this.dataSource.data = rows;
            } 
        );
}

Can you please give some guidance on how I can implement this?

Your json extract seems a little confusing, is it an array of objects? Because then you can sort the data with yourdata.sort((a, b) => a['db-nr'] - b['db-nr'])

For example:

 const data = [{ "period": 12.0, "amount": 0.0, "name": "Test1", "mandantKagId": 0.0, "db-nr": 5 }, { "period": 12.0, "amount": 0.0, "name": "Test1", "mandantKagId": 0.0, "db-nr": 2 }, { "period": 12.0, "amount": 0.0, "name": "Test1", "mandantKagId": 0.0, "db-nr": 4 }] data.sort((a, b) => a['db-nr'] - b['db-nr']) console.log(data)

Edit: To turn your JSON into a JS object you can use JSON.parse

  1. For descending order:

    const items = items.sort(function (a, b) { return b.db-nr - a.db-nr; });

  2. For ascending order:

    const items = items.sort(function (a,b) { return a.db-nr - b.db-nr; });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM