过滤器和列表角度排序的问题

时间:2020-06-05 13:45:11

标签: angular sorting filtering

我有一个按字母顺序排序的列表。然后我对其进行过滤。如果找到与模式匹配的对象,则将其索引推入indexArray中。我想使用这样的拼接将“找到的”对象移动到列表的开头:

this.indexArray.foreach(index => {
    this.sortedList.splice(0, 1 , this.sortedList.splice(indexOfFoundObject,1)[0]);
}

不幸的是,我得到的结果不是我所期望的。我得到像这样的清单: 从Z到A排序的发现对象列表(而不是A到Z) 其余列表从A到Z正确排序。 我试图制作indexArray.reverse或this.indexArray.reverse()。forEach(),但随后我根本没有得到过滤列表。如何更改代码,以便将“找到的对象”列表从A到Z排序?也许我的拼接功能有错误?

我尝试使用具有3个排序条件的1个排序函数来执行此操作,但仍无法按预期工作。

this.list.sort(function (a, b) {
   if(a.data.phaseName.toLowerCase().includes(filterParam.value.toLowerCase()) ||
      b.data.phaseName.toLowerCase().includes(filterParam.value.toLowerCase())) {
         if(a.data.phaseName.toLowerCase().includes(filterParam.value.toLowerCase()) &&                  b.data.phaseName.toLowerCase().includes(filterParam.value.toLowerCase())) {
              a.data.toExpand = true;
              a.data.toColor = true;
              b.data.toColor = true;
              b.data.toExpand = true;
              return b.data.phaseName.toLowerCase() < a.data.phaseName.toLowerCase() ? 1 : -1;
                } else if (a.data.phaseName.toLowerCase().includes(filterParam.value.toLowerCase())) {
                    a.data.toExpand = true;
                    a.data.toColor = true;
                    return 1;
                } else {
                    b.data.toColor = true;
                    b.data.toExpand = true;
                    return 1;
                }
            } if (a.data.status === b.data.status) {
                a.data.toExpand = false;
                a.data.toColor = false;
                b.data.toColor = false;
                b.data.toExpand = false;
                return b.data.phaseName.toLowerCase() < a.data.phaseName.toLowerCase() ? 1 : -1;
        }
        return b.data.status < a.data.status ? 1 : -1;
    });

所以我期望得到这样的列表排序: 1.名称按字母顺序包含给定字符串的所有数据 2.其余数据(不包含给定的字符串)按状态打开/关闭排序 3.在每种状态下,数据也应按字母顺序排序

但是我得到的是: 1.所有数据按状态打开/关闭排序 2.在每个状态下,数据也应按字母顺序排序 3.包含给定字符串的数据具有给定颜色并被扩展。 我不明白为什么第一个if的作用最不重要

0 个答案:

没有答案