How to sort an array of objects by property value in Angular with custom pipe?

aleksejjj

I need to write a custom pipe in Angular that takes an array of objects and a variable called order with a value of either ascending or descending as a parameter and then sort the array of objects by a property value.

The data looks something like this:

[
    {
        "location_type": "KAUPPA",
        "postalcode": "100",
        "availability": "LIIKETILAN AUKIOLO",
        "location": "SUOMALAINEN KIRJAKAUPPA / SISÄKÄYTÄVÄ",
        "municipality": "TURKU",
        "target_address": "ALEKSANTERINKATU 23",
        "availability_details": "",
        "coordinates_lon": "24.941095",
        "coordinates_lat": "60.168718"
    },
    {
        "location_type": "PANKIN KONTTORI",
        "postalcode": "100",
        "availability": "ITSEPALVELUALUEEN AUKIOLO",
        "location": "NORDEA SENAATINTORI",
        "municipality": "VANTAA",
        "target_address": "ALEKSANTERINKATU 30",
        "availability_details": "ma-su klo 06-22",
        "coordinates_lon": "24.950720",
        "coordinates_lat": "60.168930"
    },
    {
        "location_type": "TAVARATALO",
        "postalcode": "100",
        "availability": "LIIKETILAN AUKIOLO",
        "location": "STOCKMANN / 8. KERROS",
        "municipality": "HELSINKI",
        "target_address": "ALEKSANTERINKATU 52",
        "availability_details": "",
        "coordinates_lon": "24.941870",
        "coordinates_lat": "60.168430"
    }
]

The objects in the array need to be put into an order by municipality's value.

SiddAjmera

As mentioned several times in the Comments and on the above answer by Pardeep as well, using Pipe to sort the data is not a very good idea.

If you want to sort fields, just implement it on your template and then trigger the sort function only on events. This would significantly save you performance.

Here, give this a try:

import { Component } from '@angular/core';

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

  lastSortedByField;
  ascendingOrder = true;
  data = [
    {
      "location_type": "KAUPPA",
      "postalcode": "100",
      "availability": "LIIKETILAN AUKIOLO",
      "location": "SUOMALAINEN KIRJAKAUPPA / SISÄKÄYTÄVÄ",
      "municipality": "TURKU",
      "target_address": "ALEKSANTERINKATU 23",
      "availability_details": "",
      "coordinates_lon": "24.941095",
      "coordinates_lat": "60.168718"
    },
    {
      "location_type": "PANKIN KONTTORI",
      "postalcode": "100",
      "availability": "ITSEPALVELUALUEEN AUKIOLO",
      "location": "NORDEA SENAATINTORI",
      "municipality": "VANTAA",
      "target_address": "ALEKSANTERINKATU 30",
      "availability_details": "ma-su klo 06-22",
      "coordinates_lon": "24.950720",
      "coordinates_lat": "60.168930"
    },
    {
      "location_type": "TAVARATALO",
      "postalcode": "100",
      "availability": "LIIKETILAN AUKIOLO",
      "location": "STOCKMANN / 8. KERROS",
      "municipality": "HELSINKI",
      "target_address": "ALEKSANTERINKATU 52",
      "availability_details": "",
      "coordinates_lon": "24.941870",
      "coordinates_lat": "60.168430"
    }
  ];

  sortByField(field) {
    if(this.lastSortedByField === field) {
      this.ascendingOrder = !this.ascendingOrder;
    }
    else {
      this.lastSortedByField = field;
      this.ascendingOrder = true;
    }

    if(this.ascendingOrder) {
      this.data = this.data.sort((a, b) => {
        if (a[field] < b[field])
          return -1;
        if (a[field] > b[field])
          return 1;
        return 0;
      });
    } else {
      this.data = this.data.sort((a, b) => {
        if (a[field] < b[field])
          return 1;
        if (a[field] > b[field])
          return -1;
        return 0;
      });
    }

  }

}

And in the Template:

<table border="1">
  <thead>
    <tr>
      <td (click)="sortByField('location_type')">location_type</td>
      <td (click)="sortByField('postalcode')">postalcode</td>
      <td (click)="sortByField('availability')">availability</td>
      <td (click)="sortByField('location')">location</td>
      <td (click)="sortByField('municipality')">municipality</td>
      <td (click)="sortByField('target_address')">target_address</td>
      <td (click)="sortByField('availability_details')">availability_details</td>
      <td (click)="sortByField('coordinates_lon')">coordinates_lon</td>
      <td (click)="sortByField('coordinates_lat')">coordinates_lat</td>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let datum of data">
      <td>{{ datum.location_type }}</td>
      <td>{{ datum.postalcode }}</td>
      <td>{{ datum.availability }}</td>
      <td>{{ datum.location }}</td>
      <td>{{ datum.municipality }}</td>
      <td>{{ datum.target_address }}</td>
      <td>{{ datum.availability_details }}</td>
      <td>{{ datum.coordinates_lon }}</td>
      <td>{{ datum.coordinates_lat }}</td>
    </tr>
  </tbody>
</table>

Here's a Working Sample StackBlitz for your ref.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Sort array of objects by property length

分類Dev

How do sort array with key, value in Angular JS?

分類Dev

How do sort array with key, value in Angular JS?

分類Dev

order or sort array of objects based on a key value

分類Dev

Filter array of objects in Angular4 without pipe

分類Dev

Group array of objects by string property value in JavaScript?

分類Dev

Angular Custom Pipe not working

分類Dev

How to sort array of objects alphabetically except for numbers?

分類Dev

How can I check if the array of objects has duplicate property values and get the last value that is repeated?

分類Dev

Sorting an array based on property value of another array of objects

分類Dev

How to extract a property from array of objects and slice it?

分類Dev

Sort dictionary by value property

分類Dev

javascript return property value from nested array of objects based on condition

分類Dev

Accesing object value in array of objects by conditioning another object property

分類Dev

How to sort mongoose populated array by a value?

分類Dev

Disable the default keyvalue pipe sort in angular

分類Dev

Sort array of objects based on inner array of objects

分類Dev

How to merge Array of Objects based on the same value?

分類Dev

How to search for a value in array of objects and get it in Laravel?

分類Dev

how do i sort an array of objects via the comparable interface?

分類Dev

Javascript: Sorting an array of objects based on a timestamp property doesn't sort one element correctly

分類Dev

how to sort multi dimensional array in PHP using array value?

分類Dev

Lodash sort/order array by property

分類Dev

Sort by multiple objects in a array in Javascript

分類Dev

PHP array sort by value

分類Dev

Search MongoDB Array of Objects where property has same value for more than 1 array element

分類Dev

How create custom pipe in angular 4 with some resources to format number (accounting.js)

分類Dev

How to read array of custom objects from a plist file?

分類Dev

How to rank objects by property?

Related 関連記事

  1. 1

    Sort array of objects by property length

  2. 2

    How do sort array with key, value in Angular JS?

  3. 3

    How do sort array with key, value in Angular JS?

  4. 4

    order or sort array of objects based on a key value

  5. 5

    Filter array of objects in Angular4 without pipe

  6. 6

    Group array of objects by string property value in JavaScript?

  7. 7

    Angular Custom Pipe not working

  8. 8

    How to sort array of objects alphabetically except for numbers?

  9. 9

    How can I check if the array of objects has duplicate property values and get the last value that is repeated?

  10. 10

    Sorting an array based on property value of another array of objects

  11. 11

    How to extract a property from array of objects and slice it?

  12. 12

    Sort dictionary by value property

  13. 13

    javascript return property value from nested array of objects based on condition

  14. 14

    Accesing object value in array of objects by conditioning another object property

  15. 15

    How to sort mongoose populated array by a value?

  16. 16

    Disable the default keyvalue pipe sort in angular

  17. 17

    Sort array of objects based on inner array of objects

  18. 18

    How to merge Array of Objects based on the same value?

  19. 19

    How to search for a value in array of objects and get it in Laravel?

  20. 20

    how do i sort an array of objects via the comparable interface?

  21. 21

    Javascript: Sorting an array of objects based on a timestamp property doesn't sort one element correctly

  22. 22

    how to sort multi dimensional array in PHP using array value?

  23. 23

    Lodash sort/order array by property

  24. 24

    Sort by multiple objects in a array in Javascript

  25. 25

    PHP array sort by value

  26. 26

    Search MongoDB Array of Objects where property has same value for more than 1 array element

  27. 27

    How create custom pipe in angular 4 with some resources to format number (accounting.js)

  28. 28

    How to read array of custom objects from a plist file?

  29. 29

    How to rank objects by property?

ホットタグ

アーカイブ