According to the Angular documentation, there is currently no orderBy pipe available for sorting. It is recommended to implement the sort functionality in the component. However, as a beginner in Angular, I am unsure of how to do this. Can anyone provide assistance? I am specifically looking for the code that will help me achieve this.
Here is the code for the component.ts file, where I want to sort the indianStates array based on the viewValue:
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'addressentry',
templateUrl: './addressentry.component.html',
styleUrls: ['./addressentry.component.css']
})
export class AddressentryComponent implements OnInit {
constructor() { }
ngOnInit() {}
indianStates: IndianStates[] = [
{value: 'westbengal', viewValue: 'West Bengal'},
{value: 'sikkim', viewValue: 'Sikkim'},
{value: 'assam', viewValue: 'Assam'}
];
}
export interface IndianStates {
value: string;
viewValue: string;
}
HTML Code:
<div class="col form-group">
<mat-form-field>
<mat-label >State</mat-label>
<mat-select>
<mat-option *ngFor="let state of indianStates" [value]="state.value">
{{state.viewValue}}
</mat-option>
</mat-select>
</mat-form-field>
</div>