Following is my JSON data
{
"items": [
{
"id": 26,
"email": "[email protected]",
"firstName": "Poornima ",
"lastName": "karuppu",
"role": "Student",
"studentDetails": {
"discipline": "History",
"currentDegree": "Master",
"currentSemester": 58
},
"fullName": "Poornima karuppu"
},
{
"id": 149,
"email": "[email protected]",
"firstName": "raj",
"lastName": "naga",
"role": "Student",
"studentDetails": {
"discipline": "German Lingustics",
"currentDegree": "Master",
"currentSemester": 5
},
"fullName": "raj naga"
},
{
"id": 134,
"email": "[email protected]",
"firstName": null,
"lastName": null,
"role": "Student",
"studentDetails": {
"discipline": "History",
"currentDegree": "Master",
"currentSemester": 15
},
"fullName": " "
},
{
"id": 20,
"email": "[email protected]",
"firstName": "null",
"lastName": "null",
"role": "Student",
"studentDetails": {
"discipline": "History and Arts",
"currentDegree": "Master",
"currentSemester": 4
},
"fullName": "null null"
},
{
"id": 184,
"email": "[email protected]",
"firstName": "Rob",
"lastName": "Pat",
"role": "Student",
"studentDetails": {
"discipline": "Computer Science",
"currentDegree": "Bachelor",
"currentSemester": 25
},
"fullName": "Rob Pat"
},
{
"id": 151,
"email": "[email protected]",
"firstName": null,
"lastName": null,
"role": "Student",
"studentDetails": {
"discipline": "Art",
"currentDegree": "Master",
"currentSemester": 5
},
"fullName": " "
},
{
"id": 3,
"email": "[email protected]",
"firstName": "Lamija",
"lastName": "Halvadzija",
"role": "Student",
"studentDetails": {
"discipline": "Lingustics",
"currentDegree": "Master",
"currentSemester": 5
},
"fullName": "Lamija Halvadzija"
},
{
"id": 25,
"email": "[email protected]",
"firstName": "Rolans",
"lastName": "Mustermann",
"role": "Student",
"studentDetails": {
"discipline": "Linguistics",
"currentDegree": "Bachelor",
"currentSemester": 2
},
"fullName": "Rolans Mustermann"
},
{
"id": 178,
"email": "[email protected]",
"firstName": null,
"lastName": null,
"role": "Student",
"studentDetails": null,
"fullName": " "
},
{
"id": 140,
"email": "[email protected]",
"firstName": "Nilakshi",
"lastName": "Naphade",
"role": "Student",
"studentDetails": null,
"fullName": "NN"
},
{
"id": 40,
"email": "[email protected]",
"firstName": "P",
"lastName": "K",
"role": "Student",
"studentDetails": null,
"fullName": "PK"
}
],
}
Its a students' list that I am dispalying on UI using GET API. I am sorting these records using angular 2 pipe. Following is sort.pipe.ts code:
import { Injectable, Pipe, PipeTransform } from '@angular/core';
import { User } from '../../../core/user/user.model';
@Pipe({
name: 'hipUsersSorter'
})
@Injectable()
export class UsersSorter implements PipeTransform {
transform(users: any, key: string, direction: number): User[] {
if (key !== '' && users !== null) {
users.sort(
(a: any, b: any) => {
if (a[key] < b[key]) {
return -1 * direction;
} else if (a[key] > b[key]) {
return 1 * direction;
} else {
return 0;
}
}
);
}
return users;
}
}
Using this pipe, I am able to sort data based on firstName, lastName and email fields. However, I am not able to sort the records based on nested attributes viz. discipline, currentDegree and currentSemester. Following is HTML template from where I am calling this sort pipe:
<table>
<thead>
<tr>
<th (click)="sort('lastName')">{{ 'last name' | translate }}</th>
<th (click)="sort('firstName')">{{ 'first name' | translate }}</th>
<th (click)="sort('email')">{{ 'email' | translate }}</th>
<th></th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of students | hipUsersFilter: query: selectedOption: selectedRole | hipUsersSorter: key: direction
| paginate: { id: 'server', itemsPerPage: 10, currentPage: _page, totalItems: _total }">
<td>{{ user.lastName }}</td>
<td>{{ user.firstName }}</td>
<td>{{ user.email }}</td>
<td>{{ user.studentDetails.discipline }}</td>
<td>{{ user.studentDetails.currentDegree }}</td>
<td>{{ user.studentDetails.currentSemester }}</td>
</tr>
</tbody>
</table>
This is my sort function in component:
direction = -1;
sort(value: string) {
this.direction = this.direction * -1;
this.key = value;
}
How can I sort the data based on these nested fields? Can someone please provide their inputs on this issue? Thanks in advance