Following is a JSON data:
{
"tagFrequency": [
{
"value": "At",
"count": 1,
"tagId": 249
},
{
"value": "ipsum",
"count": 1,
"tagId": 251
},
{
"value": "molestie",
"count": 1,
"tagId": 199
}
]
}
I am populating this data on UI in a table having column names as word, frequency for above JSON object attributes value and count respectively. Third column tag-name is being extracted with GET API call using tagId attribute. Following is my HTML code:
<table>
<thead>
<tr>
<th (click)="sort('value')">{{ 'word' | translate }}</th>
<th (click)="sort('tagId')">{{ 'tag-name' | translate }}</th>
<th (click)="sort('count')">{{ 'frequency' | translate }}</th>
<th></th>
</tr>
</thead>
<tbody>
<tr *ngFor="let frequency of (frequencies | TagFrequencySorter: key: direction); let i = index;">
<td>{{ frequency.value }}</td>
<td>{{ processedTagNames[i] }}</td>
<td>{{ frequency.count }}</td>
</tr>
</tbody>
</table>
I want to sort these columns value and count, which is working with "TagFrequencySorter" pipe. But I also want to sort tagNames array data using a same pipe in same for loop. I can make required changes in pipe, but all I want is to pass these both arrays somehow to this pipe.
Following is a sort function I write in component:
sort(value: string) {
this.direction = this.direction * (-1);
if(value === "tagId") {
this.key = "";
}
else {
this.key = value;
}
}
And here is pipe implementation:
export class TagFrequencySorter implements PipeTransform {
transform(tagFrequencies: any, key: string, direction: number): any[] {
if (key !== '' && tagFrequencies !== null) {
console.log(key)
tagFrequencies.sort(
(a: any, b: any) => {
let propertyA: number|string = this.getProperty(a, key);
let propertyB: number|string = this.getProperty(b, key);
if (propertyA < propertyB) {
return -1 * direction;
} else if (propertyA > propertyB) {
return 1 * direction;
} else {
return 0;
}
}
);
}
return tagFrequencies;
}
private getProperty(value: { [key: string]: any}, key: string): number|string {
if (value === null || typeof value !== 'object') {
return undefined;
}
let keys: string[] = key.split('.');
let result: any = value[keys.shift()];
for (let newkey of keys) {
if (result === null) {
return undefined;
}
result = result[newkey];
}
return result;
}
}
Can someone help me to resolve this issue?