【问题标题】:Angular sort result of service服务的角度排序结果
【发布时间】:2020-07-09 21:25:59
【问题描述】:

我必须对 post 函数的结果进行排序。 有post功能:

    findQuotation(queryParams: QueryParamsModel): Observable<QueryResultsModel> {
    return this.http.post<QueryResultsModel>(API_QUOTATIONS_URL + '/list', queryParams);}

这些返回给我一个对象数组,比如 this

我在ngrx效果中这样调用服务的函数:

    @Effect()
loadRolesPage$ = this.actions$
    .pipe(
        ofType<QuotationsPageRequested>(QuotationActionTypes.QuotationsPageRequested),
        mergeMap(({ payload }) => {
            this.store.dispatch(this.showPageLoadingDistpatcher);
            const requestToServer = this.quotationService.findQuotation(payload.page);
            const lastQuery = of(payload.page);
            return forkJoin([requestToServer, lastQuery]);
        }),
        map(response => {
            console.log('response', response);
            const result: QueryResultsModel = response[0];
            const lastQuery: QueryParamsModel = response[1];
            this.store.dispatch(this.hidePageLoadingDistpatcher);

            return new QuotationsPageLoaded({
                quotations: result.items,
                totalCount: result.totalCount,
                page: lastQuery
            });
        }),
    );

我想根据最近的 created_at(Date object) 对这个服务器响应进行排序。

【问题讨论】:

  • 按这些排序??日期或报价单或 ID??
  • 并且还包括你在服务中调用这个函数的代码..
  • @Supercool。对不起,我剪掉了我的一部分帖子。我想按 created_at(日期)排序

标签: javascript arrays angular sorting object


【解决方案1】:

你试过了吗?

订阅服务功能后

result.sort((a,b)=> new Date(b.created_at).valueOf() - new Date(a.created_at).valueOf());

运行这个 sn-p 来检查它是否有效

let results=[{quotationNumber:"1122", created_at:"2020-03-08T01:00:00.000Z"},{quotationNumber:"1522", created_at:"2020-03-08T09:00:00.000Z"},{quotationNumber:"1222", created_at:"2020-03-01T02:00:00.000Z"},{quotationNumber:"1322", created_at:"2020-03-05T08:00:00.000Z"},{quotationNumber:"1422", created_at:"2020-02-05T08:00:00.000Z"}];
results.sort((a,b)=>new Date(b.created_at).valueOf() - new Date(a.created_at).valueOf());
console.log(results);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • 感谢您的回复!我收到此错误:'算术运算的左侧必须是'any'、'number'、'bigint'或枚举类型'
  • 是的,它有效!我现在遇到了 TypeScript 的另一个问题,但我最初的问题已经解决了。非常感谢 Supercool :)
【解决方案2】:

sort() 方法对数组的元素进行就地排序并返回排序后的数组。默认排序顺序是升序,将元素转换为字符串,然后比较它们的 UTF-16 代码单元值序列。

arr.sort([compareFunction])

compareFunction 指定定义排序顺序的函数。如果省略,则将数组元素转换为字符串,然后根据每个字符的 Unicode 码位值进行排序。

如果未提供 compareFunction,则所有非undefined 数组元素将通过将它们转换为字符串并以 UTF-16 代码单元顺序比较字符串来进行排序。例如,“香蕉”出现在“樱桃”之前。在数字排序中,9 在 80 之前,但由于数字被转换为字符串,因此在 Unicode 顺序中,“80”在“9”之前。所有 undefined 元素都排序到数组的末尾

如果提供compareFunction,则所有未定义的数组元素根据比较函数的返回值排序(所有未定义的元素都排序到数组的末尾,不调用 compareFunction)。如果 a 和 b 是两个被比较的元素,那么:

  • 如果 compareFunction(a, b) 返回小于 0,则将 a 排序到低于 b 的索引(即 a 先出现)。
  • 如果compareFunction(a, b) 返回0,则保持ab 彼此保持不变,但对所有不同元素进行排序。注意:ECMAscript 标准不保证这种行为,因此,并非所有浏览器(例如,至少可以追溯到 2003 年的 Mozilla 版本)都尊重 这个。
  • 如果 compareFunction(a, b) 返回大于 0,则将 b 排序到低于 a 的索引(即 b 排在第一位)。
  • compareFunction(a, b) 在给定一对特定元素 a 和 b 作为其两个参数时,必须始终返回相同的值。如果返回不一致的结果,则排序顺序未定义。

所以,比较函数的形式如下:

function compare(a, b) {
  if (a is less than b by some ordering criterion) {
    return -1;
  }
  if (a is greater than b by the ordering criterion) {
    return 1;
  }
  // a must be equal to b
  return 0;
}

要比较数字而不是字符串,比较函数可以简单地从 a 中减去 b。以下函数将对数组进行升序排序(如果它不包含 Infinity 和 NaN):

function compareNumbers(a, b) {
  return a - b;
}

因此,在您的情况下,new Date() 将您的字段转换为 DategetTime() 将其转换为数字(getTime() 方法返回自 Unix 纪元以来的毫秒数* . getTime() 总是使用 UTC 表示时间。例如,一个时区的客户端浏览器,getTime() 将与任何其他时区的客户端浏览器相同。)

既然你有数字,我们将使用compareNumbers 作为compareFunction 结果:

 const quotations = result.items.sort((a,b)=> new Date(b.created_at).getTime() - new Date(a.created_at).getTime());

return new QuotationsPageLoaded({
   quotations: quotations,
   totalCount: result.totalCount,
   page: lastQuery
});

【讨论】:

  • 仅代码的答案几乎总是可以通过添加对代码工作方式和原因的解释来改进。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-01-24
  • 2018-10-10
  • 2018-10-04
  • 2017-01-26
  • 2022-01-14
  • 2018-07-31
  • 1970-01-01
相关资源
最近更新 更多