简体   繁体   English

如何对从ngrx-entities检索的数据进行排序

[英]How to sort data retreived from ngrx-entities

I have to sort data fetched from the ngrx-entities-store.我必须对从 ngrx-entities-store 获取的数据进行排序。 I'm not intending to do it inside the reducer because I have three sorting methods to implement and I followed the accepted answer on this stackoverflow post to try to make it done, but I'm kinda hitting the walls.我不打算在减速器内执行此操作,因为我要实现三种排序方法,并且我按照此 stackoverflow 帖子上接受的答案尝试完成它,但我有点碰壁了。 The unsorted data are well fetched using this snippet:使用此代码段可以很好地获取未排序的数据:

sortPersnlByFunction(sortBy: string){
    this.filterDefault = false;
    this.filterByStatus = false;
    this.filterByFunction = true;
    this.zone.run(() => {
    this.store
      .pipe(select(selectAllPrsnls), map((prsnlList) => prsnlList))
      .subscribe(value => {
        this.Prsnl = Object.entries(value[1]).map((e) => ( e[1]  ));
        console.log("The Prsnl", this.Prsnl)
    });   
  })}

The console shows that the data (not sorted) is successfully fetched, and I then added this fragment to perform the sorting:控制台显示数据(未排序)已成功获取,然后我添加了这个片段来执行排序:

this.sortedPrsnl = [...this.Prsnl].sort(
          (a, b) => a[sortBy].localCompare(b[sortBy]));

Now the overall code is like this:现在整体代码是这样的:

sortPersnlByFunction(sortBy: string){
    this.filterDefault = false;
    this.filterStatus = false;
    this.filterFunction = true;
    this.zone.run(() => {
    this.store
      .pipe(select(selectAllPrsnls), map((prsnlList) => prsnlList))
      .subscribe(value => {
        this.Prsnl = Object.entries(value[1]).map((e) => ( e[1]  ));
        this.sortedPrsnl = [...this.Prsnl].sort(
          (a, b) => a[sortBy].localCompare(b[sortBy]));
        console.log("The sorted", this.sortedPrsnl)
    });
    
  })}

But adding the sorting fragment does not work either, nor the console.log method triggered.但是添加排序片段也不起作用,也没有触发 console.log 方法。 It seems like the last fragment failed to execute, and so the console.log wasn't even reached to display anything at all.似乎最后一个片段未能执行,因此甚至没有到达 console.log 以显示任何内容。

I just want to mention that the sortBy is a criteria string sent from the frontend, and is the keyword by which the sorting should be applied to.我只想提一下, sortBy是从前端发送的条件字符串,并且是应用排序的关键字。 It's a Prsnl data property.这是一个 Prsnl 数据属性。 I'm gratefull to any help to make this done, thanks in advance.我很感谢任何帮助来完成这项工作,在此先感谢。

Since the last console.log is not showing anything, you know that your fragment由于最后一个 console.log 没有显示任何内容,您知道您的片段

this.sortedPrsnl = [...this.Prsnl].sort(
          (a, b) => a[sortBy].localCompare(b[sortBy]));

is not working.不管用。 And a possible raison for that is because the sortby parameter is not used, and can not be used that way.一个可能的原因是因为sortby参数没有被使用,也不能那样使用。 So I will suggest to you another approach to avoid confusion.因此,我将向您建议另一种避免混淆的方法。 instead of localeCompare go ahead and compare the elements yourself, with javascript basic string comparison operators.而不是localeCompare go 提前并自己比较元素,使用 javascript 基本字符串比较运算符。 Then, assuming that myField is the filed property you wanted to sort with, your fragment will become this:然后,假设myField是您想要排序的字段属性,您的片段将变为:

this.sortedPrsnl = [...this.Prsnl].sort(
              (a, b) => (a.myField.toLowerCase() > b.myField.toLowerCase())? 1 : -1;

With this approach you sort this.Prsnl in alphabetic order of field myField which should be a field property of this.Prsnl .使用这种方法,您可以按字段myField的字母顺序对 this.Prsnl 进行排序,这应该是this.Prsnl的字段属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM