#angular
#angular
Вопрос:
Как вы можете видеть в файле .ts, я пытаюсь выполнить сортировку массива, который находится в табличной форме. Массив находится в статическом
list.component.ts
sort(){
this.Homes.sort(function(a,b){
if(a>b)
return 1
if(a<b)
return -1
return 0;
});
}
list.component.html
<table class="table table-striped">
<thead>
<tr>
<th scope="col" (click)="sort()">Name</th>
<th scope="col">Age</th>
<th>Action</th>
</tr>
</thead>
<tbody *ngFor="let item of Homes">
<!-- <tr *ngIf="item.Age < 22"> -->
<tr>
<td scope="row">{{ item.Name }}</td>
<td class={{item.ColorClass}}>{{item.Age}}</td>
<td class="fa fa-trash" (click)="delete(item)"></td>
<td class="fa fa-pencil" (click)="edit(item)" data-toggle="modal" data-
target="#myModal"></td>
</tr>
</tbody>
</table>
Ответ №1:
Вы должны указать имя свойства объекта, т.е.
sort(){
this.Homes.sort(function(a,b){
if(a.Name > b.Name)
return 1
if(a.Name < b.Name)
return -1
return 0;
});
}
Ответ №2:
На основе ответа Мукеша Вадодарии, но проще
sort(){
this.Homes.sort(function(a,b){
return a.Name.localCompare(b.Name);
});
}