首页 > 解决方案 > 角度排序列

问题描述

现在这就是我正在使用的东西,因为我无法让它工作。无论如何,我认为这并不坏,但仍然:

在我的 app.component.html 我有:

    <table border="1">
        <!-- ADD HEADERS -->
        <tr>
            <th><a href="#" (click)="send()">Name</a>
            </th>
            <th>Goverment form</th>

        </tr>

    </table>

在我的 app.component.ts 我有:

         }
         return 0;
    });
     }



--------
Data:
-------

    export interface Country {
        Name:string;
        GovernmentForm:string;

    }
    export class AppComponent {
      public Countries: Country[];

标签: javascriptangulartypescriptsorting

解决方案


您需要更改排序函数来比较.Name属性,而不是整个对象。你可以这样做:

this.Countries = this.Countries.sort((n1, n2) => {
  if (n1.Name > n2.Name) {
    return 1;
  }
  if (n1.Name < n2.Name) {
    return -1;
  }
  return 0;
});

推荐阅读