【问题标题】:How can I sort by alphabetical order in Angular 11?如何在 Angular 11 中按字母顺序排序?
【发布时间】:2021-05-17 15:36:42
【问题描述】:

这应该是一个简单的任务,但是,我是一个新的 Angular 用户,我需要通过 API HTTP Get 调用按字母顺序对我的对象进行排序。

这是我从 API 检索数据的服务:

getCustomer(key: string, page: number, pageSize: number): Observable<PagedResponse<Customer>> {
  return this.http
    .get<PagedResponse<Customer>>(environment.UrlApiSubscription + "Customer/" + key + "/" + page + "/" + pageSize, super.GetAuthTokenHeader())
    .pipe(
      map(super.extract),
      catchError(super.serviceError)
    )
}

这是我的基础服务:

protected extract(respose: any) {
    return respose || {};
}

protected serviceError(response: Response | any) {
    let customError: string[] = [];

    if (response instanceof HttpErrorResponse) {
      if (response.statusText === "Unknown Error") {
        customError.push("Error");
        response.error.errors = customError;
      }
    }

    console.error(response);
    return throwError(response);
}

【问题讨论】:

  • base service代码是什么原因
  • “基础服务”是我提取数据的地方。也许这是我需要改变的地方。只是因为它。如果对您没有意义,请忽略。

标签: angular typescript angular11


【解决方案1】:

您的问题对我来说真的不清楚,但我可以建议对您的回复进行排序,您可以参考下面的代码,其中属性将是您的属性,您希望在此基础上按字母顺序对数组结果进行排序

response.sort((a, b) => (a.attribute < b.attribute ) ? -1 : 1)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 2020-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多